Please refer to the code below:
#include <algorithm>
namespace N
{
template <typename T>
class C
{
public:
void SwapWith(C & c)
{
using namespace std; // (1)
//using std::swap; // (2)
swap(a, c.a);
}
private:
int a;
};
template <typename T>
void swap(C<T> & c1, C<T> & c2)
{
c1.SwapWith(c2);
}
}
namespace std
{
template<typename T> void swap(N::C<T> & c1, N::C<T> & c2)
{
c1.SwapWith(c2);
}
}
As written above, the code doesn't compile on Visual Studio 2008/2010. The error is:
'void N::swap(N::C<T> &,N::C<T> &)' : could not deduce template argument for 'N::C<T> &' from 'int'.
However, if I comment out (1) and uncomment (2), it will compile OK. What is the difference between using namespace std
and using std::swap
that explains this behavior?
N::swap
instead ofstd::swap
even if you haveusing namespace std
– Pawsnerstd
namespace, only specializations. – Draff