I've read that when you're swap
ing things in c++, you should always using std::swap;
, then call swap
unqualified, so it automatically picks the std::
ones for std::
and builtin types, your custom one for custom types, and the templated std::
one for everything else.
So, can I just put using std::swap;
in the header that every file includes and not have to worry about it?
I understand that avoiding using
in a header is common practice. However, is there a problem with it in this particular case?
The guidance for swap is to using std::swap
at the most local scope possible. For certain, one in a header file that's widely included does not meet this requirement. It still pollutes the global namespace in unexpected ways (someone not expecting std::swap
will be imported to the global namespace) and should be avoided just like using namespace
.
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