Is this nested namespace syntax standard in C++?

  • A+
Category:Languages

Is this standard in C++? In C#, I liked declaring nested namespaces like this:

namespace A.B  {     class X     {     }; } 

The alternative was this, which is a little uglier:

namespace A {     namespace B     {         class X         {         };     } } 

In C++, I wanted to see if it had a similar feature. I ended up finding this works:

namespace A::B {     class Vector2D     {     } } 

Notice the ::.

I'm curious if this is standard C++ or if this is a MS feature. I can't find any documentation on it. My ancient C++98 reference book doesn't mention it, so I wonder if it's an extension from Microsoft or a new feature.

 


Yes, this is legal C++ 17 syntax. It is, however, not called embedded namespace, but nested namespace.

namespace ns_name::name (8) (since C++17)

[...]

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: