title: Does swap() cause undefined behaviour?
body: <p>I'm trying to understand the conditions on <code>std::swap</code> from [C++11: utility.swap]. The template is defined as</p>
  
  <pre><code>template &lt;typename T&gt; void swap(T &amp;, T &amp;)
  </code></pre>
  
  <p>(plus some <code>noexcept</code> details) and as having the effect of "exchanging the values stored at the two locations".</p>
  
  <p>Is the following program have well-defined?</p>
  
  <pre><code>#include &lt;utility&gt;
  
  int main()
  {
      int m, n;
      std::swap(m, n);
  }
  </code></pre>
  
  <p>If I wrote swap code myself (i.e. <code>int tmp = m; m = n; n = tmp;</code>), it would have undefined behaviour, since it would attempt lvalue-to-rvalue conversion on an uninitialized object. But the standard <code>std::swap</code> function does not seem to come with any conditions imposed on it, nor can one derive from the specification that there is any lvalue-to-rvalue and thus UB. </p>
  
  <p>Does the standard require <code>std::swap</code> to perform some magic that is well-defined on uninitialized objects?</p>
  
  <p>To clarify the point, consider the function <code>void f(int &amp; n) { n = 25; }</code>, which never has undefined behaviour (since it does not read from <code>n</code>).</p>