c++ namespace best practice dilemma

后端 未结 6 1994
遥遥无期
遥遥无期 2021-01-31 03:55

I\'m finding that what I\'ve considered \"best practice\" for use namespace in c++ is hurting the readability of my code, and making me question how to use them well.

My

6条回答
  •  花落未央
    2021-01-31 04:23

    I have been in this situation before. It is often the case that a lot of template functions/classes in your headers are really "implementation", although by the nature of templates in C++ you are forced to put them in a header file. Thus, I just put everything in some "detail" or "implementation" namespace, where I can comfortably use "using namespace". At the end, I "drop" what people should use to the corresponding place. Like this:

    namespace myproject { namespace somemodule {
    
    namespace _implementation {
    
    using namespace myproject::othermodule;
    using namespace myproject::yetanothermodule;
    
    template <...>
    class some_internal_metafunction{
    ...
    };
    
    template <...>
    class stuff_people_should_use_outside {
    ...
    };
    
    } // namespace implementation       
    
    using stuff_people_should_use_outside ;
    }} // namespace myproject::somemodule
    

    This approach might enlarge a bit the names on your compiler reports, though.

    Alternatively, you can give up the modules namespaces. But it might not be a good idea for an extremely large project.

提交回复
热议问题