4

From my understanding, appending :: to the front of a namespace refers to the global namespace, regardless of any using statements or parent namespaces. If that is the case, and I haven't mis-understood anything, then why does such code compile (at least in Visual Studio):

namespace Foo {
    namespace Bar {
        class X;
    }
}

using namespace Foo;
int main(void)
{
    ::Bar::X x;
}
4

1 回答 1

6

using namespace Foo; brings all the contents of namespace Foo into the context of the current namespace.

Since namespace Bar is among the contents of namespace Foo and the current namespace on the line with the using statement is the global namespace, namespace Bar is brought into the context of the global namespace.

于 2013-08-09T20:51:22.060 回答