Beachwalker
Beachwalker

Reputation: 7915

How to shorten the namespace indentation in C++ header files without using 'using'?

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++?

Upvotes: 0

Views: 1364

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171293

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

Upvotes: 5

Luchian Grigore
Luchian Grigore

Reputation: 258608

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.

Upvotes: 2

Related Questions