C++ namespaces advice

前端 未结 9 1186
长发绾君心
长发绾君心 2020-11-28 21:48

I\'m just teaching myself C++ namespaces (coming from a C# background) and I\'m really starting to think that even with all the things that C++ does better than most other l

9条回答
  •  温柔的废话
    2020-11-28 22:12

    what I do when forward declaring looks like this:

     namespace abc { namespace sub { namespace subsub { class MyClass; }}}
    

    My forward declarations are collapsed into single line. Readability of forward declaration is sacrificed in return for the readability of the rest of code. And for definitions I don't use indentations either:

     namespace abc {
     namespace sub {
     namespace subsub {
     
     class MyClass 
     {
        public:
           MyClass();
    
           void normalIntendationsHere() const;
     };
    
     }
     }
     }
    

    Using this style requires a bit of discipline at the beginning, but it's the best compromise for me.


    Since C++17 you can declare namespace with a syntax proposed by the author of the question.

    namespace A::B::C { ... }
    

    nested namespace definition: namespace A::B::C { ... } is equivalent to namespace A { namespace B { namespace C { ... } } }.

    https://en.cppreference.com/w/cpp/language/namespace

提交回复
热议问题