Is it wrong to use C++ 'using' keyword in a header file?

前端 未结 5 723
[愿得一人]
[愿得一人] 2020-12-19 00:28

I\'ve been told it\'s bad to have \"using namespace ns123\" in a header file, but I can\'t remember what the reason given was. Is it in fact a bad thing to do, and why?

5条回答
  •  囚心锁ツ
    2020-12-19 00:52

    If you put a using declaration in a header file, anything that #includes the header file also has the namespace imported, whether they want it or not. This violates the principle of least surprise and defeats the purpose of namespaces by allowing changing an #include statement to easily cause a naming collision. If you want to import a namespace in your own .cpp file to save a little typing and produce more readable code, that's fine. Just don't force users of your module to do the same.

提交回复
热议问题