C++17: Nested Namespaces

The second post in this series of C++17 features highlights a tiny but very useful new feature called nested namespaces.

Using multi-level namespaces in C++ has always been a pain. Suppose you are writing a game and you have a namespace called Core::Graphics::Rendering. Before C++17 you had to resort to something as follows

1
2
3
4
5
6
7
8
9
10
11
namespace Core {
    namespace Graphics {
        namespace Rendering {
 
            class PostProcessor
            {
            };
 
        }
    }
}

This leaves you with a very big indentation scheme. You could try to make it look a bit better as follows:

1
2
3
4
5
6
7
namespace Core { namespace Graphics { namespace Rendering {
 
    class PostProcessor
    {
    };
 
}}}

But this does not always plays nice with the auto formatting functionality of your IDE.

Say hello to C++17 nested namespaces. Now you can simply write the following:

1
2
3
4
5
6
7
namespace Core::Graphics::Rendering {
 
    class PostProcessor
    {
    };
 
}

Nested namespaces are supported in Microsoft Visual C++ 2015 since Update 3.

Share

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: