Untitled and named namespace resolution

Should a reference to a name that exists in both the unnamed namespace and the local named namespace result in an ambiguity error or is the resolution clearly defined? I see that the next job works fine on g ++ and Clang, less well 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(); return 0; } 
+5
source share
1 answer

GCC and Clang are right. In Foo::tester unqualified use of Bar clearly refers to Foo::Bar .

An unqualified search is given by C ++ 11 3.4.1 / 1:

to search for areas, a declaration is used in the order indicated in each of the corresponding categories; the name search ends as soon as an ad is found for the name.

The areas used to use a name in a function are listed in section 3.4.1 / 6:

The name used in the definition of the function [...], which is a member of the namespace N [...], must be declared before its use in the block [...] or must be declared before its use in the namespace N or, if N is a nested namespace, must be declared before it is used in one of the Ns spanning namespaces.

In this case, the function is a member of Foo , so the search for Foo is performed before the enclosing (global) namespace, which includes the unnamed namespace. Foo::Bar is there, and the search ends.

+3
source

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


All Articles