“using namespace” in c++ headers

后端 未结 9 2178
失恋的感觉
失恋的感觉 2020-11-22 01:32

In all our c++ courses, all the teachers always put using namespace std; right after the #includes in their .h files. This seems to me

9条回答
  •  礼貌的吻别
    2020-11-22 01:49

    I believe you can use 'using' in C++ headers safely if you write your declarations in a nested namespace like this:

    namespace DECLARATIONS_WITH_NAMESPACES_USED_INCLUDED
    {
        /*using statements*/
    
        namespace DECLARATIONS_WITH_NO_NAMESPACES_USED_INCLUDED
        {
            /*declarations*/
        }
    }
    
    using namespace DECLARATIONS_WITH_NAMESPACES_USED_INCLUDED::DECLARATIONS_WITH_NO_NAMESPACES_USED_INCLUDED;
    

    This should include only the things declared in 'DECLARATIONS_WITH_NO_NAMESPACES_USED_INCLUDED' without the namespaces used. I have tested it on mingw64 compiler.

提交回复
热议问题