Confusion About C ++ Global Namespace

In my opinion, adding :: to the top of the namespace refers to the global namespace, regardless of any use of operators or parent namespaces. If this is the case, and I didn’t understand anything, then why is this code compiled (at least in Visual Studio):

 namespace Foo { namespace Bar { class X; } } using namespace Foo; int main(void) { ::Bar::X x; } 
+4
source share
1 answer

using namespace Foo; displays the entire contents of namespace Foo in the context of the current namespace.

Since the namespace Bar is included in the namespace Foo , and the current namespace in the line with the expression using is the global namespace, the namespace Bar is entered into the context of the global namespace.

+6
source

Source: https://habr.com/ru/post/1496238/


All Articles