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]
- 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?
1. adjective. If you are unqualified, you do not have any qualifications, or you do not have the right qualifications for a particular job. She was unqualified for the job. [
A function call expression such as func(a,b,c) , in which the function is named without the :: scope operator, is called unqualified. When C++ code refers to a function by an unqualified name, the compiler performs a search for a matching function declaration.
Using directive syntaxusing namespace name ; The name must be a previously defined namespace. The using directive may be applied at the global and local scope but not the class scope. Local scope takes precedence over global scope by hiding similar declarations with some exceptions.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With