Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to put "using std::swap;" in a header?

I've read that when you're swaping 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?

like image 998
Dan Avatar asked Feb 25 '15 21:02

Dan


1 Answers

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.

like image 90
Mark B Avatar answered Oct 13 '22 06:10

Mark B