Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this nested namespace syntax standard in C++?

Tags:

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.

like image 569
Bob Avatar asked Jun 17 '18 06:06

Bob


1 Answers

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 { ... } } }.

like image 78
idmean Avatar answered Oct 27 '22 10:10

idmean