title: C++ : Swapping template class elements of different types?
body: <pre><code>template&lt; class T1, class T2 &gt;
  class Pair {
      T1 first;
      T2 second;
  };
  </code></pre>
  
  <p>I'm being asked to write a swap() method so that the first element becomes the second and the second the first. I have:</p>
  
  <pre><code>Pair&lt;T2,T1&gt; swap() {
      return Pair&lt;T2,T1&gt;(second, first);
  }
  </code></pre>
  
  <p>But this returns a new object rather than swapping, where I think it needs to be a void method that changes its own data members. Is this possible to do since T1 and T2 are potentially different class types? In other words I can't simply set temp=first, first=second, second=temp because it would try to convert them to different types. I'm not sure why you would potentially want to have a template object that changes order of its types as it seems that would cause confusion but that appears to be what I'm being asked to do.</p>
  
  <p>Edit: Thank you all for answering! Pretty much as I thought, swapping in place obviously does not make any sense, the request for the swap() function was quite ambiguous.</p>