title: Swapping storage buffers containing placement new created objects
body: <p>I recently saw a piece of code which used storage buffers to create objects and then simply swapped the buffers in order to avoid the copying overhead. Here is a simple example using integers:</p>
  
  <pre><code>std::aligned_storage_t&lt;sizeof(int), alignof(int)&gt; storage1;
  std::aligned_storage_t&lt;sizeof(int), alignof(int)&gt; storage2;
  
  new (&amp;storage1) int(1);
  new (&amp;storage2) int(2);
  
  std::swap(storage1, storage2);
  
  int i1 = reinterpret_cast&lt;int&amp;&gt;(storage1);
  int i2 = reinterpret_cast&lt;int&amp;&gt;(storage2);
  
  //this prints 2 1
  std::cout &lt;&lt; i1 &lt;&lt; " " &lt;&lt; i2 &lt;&lt; std::endl;
  </code></pre>
  
  <p>This feels like undefined behaviour in the general case (specifically swapping the buffers and then accessing the objects as if they were still there) but I am not sure what the standard says about such usage of storage and placement new. Any feedback is much appreciated!</p>