Skip to content

C++17 Nested Namespaces

Nested namespaces used to be somewhat cumbersome in C++. You had to repeat the namespace keyword and by default each namespace resulted in an extra level of indentation:

namespace pmp {
    namespace algorithms {
        namespace subdivision {
            // ...
            // your code here
            // ...
        }
    }
}

With a bit of tweaking, you could teach your IDE or clang-format to avoid the indentation:

namespace pmp {
namespace algorithms {
namespace subdivision {
    // ...
    // your code here
    // ...
}
}
}

Still, this always felt cumbersome to me.

With C++17 you can write this much more compact manner:

namespace pmp::algorithms::subdivision {
    // ...
    // your code here
    // ...
}

Admittedly, this is a relatively minor feature addition in C++17. However, now that I had a chance to use it I don’t want to miss it anymore.

Hope this helps!