Sunday 15 February 2015

c++ - Unnamed and Named Namespace Resolution -



c++ - Unnamed and Named Namespace Resolution -

should reference name exists in both unnamed namespace , local named namespace result in error ambiguity or resolution well-defined? i'm seeing next work fine on g++ , clang, less on msvc.

namespace foo { class bar { public: int x; }; } namespace { class bar { public: int y; }; } namespace foo { void tester() { bar b; } } int main() { foo::tester(); homecoming 0; }

gcc , clang right. within foo::tester, unqualified utilize of bar unambiguously refers foo::bar.

unqualified lookup specified c++11 3.4.1/1:

the scopes searched declaration in order listed in each of respective categories; name lookup ends declaration found name.

the scopes searched utilize of name in function listed in 3.4.1/6:

a name used in definition of function [...] fellow member of namespace n [...] shall declared before utilize in block [...] or, shall declared before utilize in namespace n or, if n nested namespace, shall declared before utilize in 1 of n’s enclosing namespaces.

in case, function fellow member of foo, foo searched before enclosing (global) namespace, includes unnamed namespace. foo::bar found there, , lookup ends.

c++ namespaces

No comments:

Post a Comment