Why shouldn't I put "use the std namespace" in the header?

Someone once hinted that doing this in the header file is not recommended:

using namespace std; 

Why is this not recommended?

Could this cause such linker errors: (for convenience)

 error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >:: ~basic_string<char,struct std::char_traits<char>,class std::allocator<char> > (void)" ( ??1?$basic_string@DU ?$char_traits@D @ std@ @ V?$allocator@D @ 2@ @ std@ @ QAE@XZ ) already defined in tools.lib(Exception.obj) 
+1
source share
2 answers

Because it forces anyone using your header file to bring the std to the global scope. This can be a problem if they have a class that has the same name as one of the standard library classes.

+9
source

If the file is included elsewhere, the compilation unit will implicitly receive the using directive. This can lead to confusing errors when matching names.

+1
source

Source: https://habr.com/ru/post/1380858/


All Articles