The C++ standard prohibits declaring types or defining anything in namespace std, but it does allow you to specialize standard STL templates for user-defined types.
Usually, when I want to specialize std::swap for my own custom templated type, I just do:
namespace std
{
template <class T>
void swap(MyType<T>& t1, MyType<T>& t2)
{
t1.swap(t2);
}
}
…and that works out fine. But I’m not entirely sure if my usual practice is standard compliant. Am I doing this correctly?
What you have is not a specialization, it is overloading and exactly what the standard prohibits. (However, it will almost always currently work in practice, and may be acceptable to you.)
Here is how you provide your own swap for your class template:
And here is how you call swap, which you’ll notice is used in Ex’s swap too:
Related: Function template specialization importance and necessity