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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With