title: Providing swap() for a C++ template class breaks std::swap()?
body: <p>I was trying to implement the <a href="https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom">copy-and-swap idiom</a> in my custom Matrix class, and I ran into some trouble with the implementation of swap() in the way suggested in the linked-to question:</p>
  
  <p>(The compiler I used is the one from MS VS2010 IDE, dialect is good old-fashioned C++03.)</p>
  
  <pre><code>// matrix.h
  
  namespace my_space 
  {
  
  template&lt;typename T&gt; class Matrix
  {
  public:
      /* ... */
  
      friend void swap(Matrix&lt;T&gt; &amp;first, Matrix&lt;T&gt; &amp;second)
      {
          using std::swap;
          swap(first.width_, second.width_);
          swap(first.height_, second.height_);
          swap(first.data_, second.data_);
      }
  };
  
  } // namespace
  </code></pre>
  
  <p>Now I have trouble reaching regular std::swap() in the code for functions residing in this namespace:</p>
  
  <pre><code>// some_code.cpp:
  #include "matrix.h"
  #include &lt;algorithm&gt;
  
  using namespace my_space;
  using namespace std;
  
  // SomeClass is part of my_space!
  void SomeClass::some_function()
  {
      int a = 3, b = 7;
      swap(a,b);  // I wan't std::swap!
  }
  </code></pre>
  
  <p>Unfortunately, for some reason, <code>my_space::swap()</code> for Matrix seems to alias all other calls to <code>std::swap()</code>, and I've no idea why since the arguments don't fit and ADL should favor <code>std::swap</code>:</p>
  
  <pre><code>1&gt;f:\src\some_code.cpp(653): error C3767: 'swap': candidate function(s) not accessible
  1&gt;          could be the friend function at 'f:\src\matrix.h(63)' : 'swap'  [may be found via argument-dependent lookup]
  </code></pre>
  
  <p>(The error repeats 10 times for every line where I'm trying to use <code>std::swap</code>)</p>
  
  <p>Does <code>my_space::swap()</code> always overrule <code>std::swap()</code> in <code>my_space</code>, even if the arguments don't fit? It's not as if <code>std::swap()</code> is not visible, and it worked OK before <code>my_space::swap()</code> was created.</p>