0

If you use namespaces for separation of modules / structurization the nesting and indentation within the the header file increases dramatically. Is there a way to write the following code in a shorter way?

namespace A
{
    namespace B
    {
        namespace C
        {
            namespace D
            {
                namespace E
                {
                    template <typename T>
                    public class X
                    {
                        public: ...

e.g. like

namespace A::B::C::D::E
{
  template<typename T> ...
}

in the header file in c++?

4
  • 2
    Try to avoid such a deep nesting. Commented May 22, 2012 at 12:37
  • 5
    Is there really a big difference between namespace A::B::C { and namespace A { namespace B { namespace C {? You don't have to indent everything like you do. Commented May 22, 2012 at 12:43
  • The Google C++ Style Guide recommends not indenting namespaces. Commented May 22, 2012 at 13:18
  • @Fiktik The difference is readablility (which is indeed a weakness of c++) Commented Jul 26, 2012 at 13:51

2 Answers 2

5

No, that nested namespace syntax has been suggested before at different times and places but isn't valid.

You don't need to indent though

namespace A { namespace B { namespace C {
// ...
} } } // namespace A::B::C
Sign up to request clarification or add additional context in comments.

Comments

2

You can use namespace aliasing. This doesn't work for extending existing namespaces, but rather for easier access.

You can use macros to extend existing namespaces, but it you need to do this, you've probably got a deeper namespace hierarchy than you need or want.

2 Comments

The question was in the context of declaring things in namespaces in headers and you can't use aliases for that as you can't declare things in a namespace alias. And macros? Eurgh.
Personally, I don't like marcros / preprocessor defines and try to avoid them if possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.