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

前端 未结 5 769
[愿得一人]
[愿得一人] 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:58

    Ordinary C++ Programming

    Yes there are often places where this makes sense, and you have control over where and how you use your using stamenets. If this is a program with a limited scope you do not need to worry about place usings in global scope.

    For monolithic applications I would strongly recommend not placing usings in a very busy namespace -- as collisions will drive you mad once you are committed. For example:

    1. ::
    2. ::companyname
    3. ::companyname::component
    4. ::companyname::component::sub-component.

    I would say that it is perfectly acceptable to start place using statements at level 3 or 4 -- thats where the chances of collisions start becoming very low.

    Generic / TMP Programming

    In generic programming and TMP using is often used to configure your libraries for a specific domain and this is often done through using statements.

提交回复
热议问题