title: proper way to swap unions?
body: <p>I have a class which has a union as one of its members. I also am a big fan of the <a href="http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap" rel="nofollow">copy/swap idiom</a>. It occurred to me that there doesn't appear to be any correct (in the sense of the standard) to swap unions!</p>
  
  <p>Here's the best that I could come up with:</p>
  
  <pre><code>char tmp[sizeof(U)];
  memcpy(tmp, &amp;u1, sizeof(U));
  memcpy(&amp;u1, &amp;u2, sizeof(U));
  memcpy(&amp;u2, tmp, sizeof(U));
  </code></pre>
  
  <p>Since unions (at least in c++03) require that all members be POD, I don't see why this wouldn't work. But it just doesn't <strong>feel</strong> right. Is there a more correct way to swap unions? It seems almost like something that was overlooked.</p>
  
  <p>What do you guys think?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>OK, I feel kinda dumb after seeing the solutions given :-). I wrote off traditional solutions like <code>std::swap</code> and assignment because when I first wrote the code, the union was an anonymous union. In the current version of the code, the union is no longer anonymous and traditional solutions seem to work just fine. Thanks.</p>