Search for an unqualified name: why a local declaration hides the declaration from using the directive

Consider this code:

namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } 

And basic.lookup.unqual.2 :

ยง6.4.1 Search for an unqualified name [basic.lookup.unqual]

  1. Declarations from the namespace assigned using the using directive become visible in the namespace including the using directive; see [Namespace.udir]. For the purposes of the unqualified name lookup rules described in [basic.lookup.unqual], declarations from the namespace assigned by the using directive are considered members that span the namespace.

For me, the standard says quite clearly that for an unqualified name lookup ( i in int k = i ), the declaration i from A is considered a member of B , so i should be ambiguous in int k = i , however, gcc and clang compile and allow i to local B::i . I searched the standard ( basic.scope.hiding and namespace.udir ) and did not find an exception or rule that contradicted the above. I found this to search for a qualified name, but not to search for an unqualified name.

Why is i unique?

+5
source share
1 answer

Key 10.3.4 / 2 "When searching for an unqualified name, names are displayed as if they were declared in the nearest spanning namespace, which contains both the directive and the assigned namespace."

The assigned namespace is A, the using directive is in B, and the smallest (in fact only) shared namespace is the global namespace. Thus, i appears as if declared in the global namespace and hides B::i .

+5
source

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


All Articles