c++ namespace best practice dilemma

后端 未结 6 1987
遥遥无期
遥遥无期 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:10

    Personally? I'd get rid of the "myproject" part. What is the chance that your library will use the exact same namespace name as another and have a symbol defined with the same name as another?

    Also, I would suggest shorter names for namespaces you expect to be used in headers.

    0 讨论(0)
  • 2021-01-31 04:21

    Here's what I do.

    In <mylibrary.h>:

    namespace myproject {
      namespace mylibrary
      {
        namespace impl
        {
          using namespace otherlibrary;
          using namespace boost;
          using namespace std;
          using namespace whatever::floats::your::boat;
    
          class myclass;
          class myotherclass;
        };
        using impl::myclass;
        using impl::myotherclass;
      };
    };
    

    In the source:

    #include <mylibrary.h>
    using namespace myproject::mylibrary; //clean!
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 04:29

    If your project isn't very very very huge (I mean, very huge), using only myproject should be sufficent. If you really want to divide your project into parts, you can use more generalized namespaces. For example, if I was building a game engine, I would go for namespaces like MyEngine::Core, MyEngine::Renderer, MyEngine::Input, MyEngine::Sound etc.

    0 讨论(0)
  • 2021-01-31 04:33

    My experience have been that it is much more convenient to have one namespace for all your code for the reasons you mentioned in your original post. This namespace protects your identifiers from clashing with identifiers from 3rd-party libraries. Your namespace is your dominion and it is easy to keep it name-conflict-free.

    0 讨论(0)
  • 2021-01-31 04:34

    I use the following to get rid of enormous amounts of std:: in header file:

    // mylibrary.h
    namespace myproject {
      namespace mylibrary {
        namespace impl {
    
          using namespace std;
    
          namespace stripped_std {
    
            // Here goes normal structure of your program:
            // classes, nested namespaces etc.
            class myclass;
            namespace my_inner_namespace {
                    ...     
            }
    
           } // namespace stripped_std   
        } // namespace impl
    
      using namespace impl::stripped_std;
    
      } // namespace mylibrary
    } namespace myproject
    
    
    // Usage in .cpp file
    #include <mylibrary.h>
    using namespace myproject::mylibrary;
    

    It is similar to what was suggested by n.m., but with a modification: there is one more auxiliary namespace stripped_std. The overall effect is that line using namespace myproject::mylibrary; allows you to refer to the inner namespace structure, and at the same time it does not bring namespace std into library user's scope.


    It's a pity though that the following syntax

    using namespace std {
    ...
    }
    

    is not valid in C++ at the time when this post is written.

    0 讨论(0)
提交回复
热议问题