Unqualified name lookup: Why local declaration hides declaration from using directive
Asked Answered
S

1

8

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 Unqualified name lookup [basic.lookup.unqual]

  1. The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.

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

Why is i unambiguous?

Solicitous answered 20/1, 2018 at 18:48 Comment(1)
this a continuation to a question from another user: #48359794Solicitous
P
8

The key is 10.3.4/2 "During unqualified name lookup, the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace."

The nominated namespace is A, the using directive is in B, and the smallest (in fact only) common namespace is the global namespace. Thus i appears as if declared in the global namespace, and is hidden by B::i.

Pheasant answered 20/1, 2018 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.