不合格的名称查找:为什么局部声明隐藏使用指令的声明

Unqualified name lookup: Why local declaration hides declaration from using directive

考虑这段代码:

namespace A
{
   int i = 24;
}

namespace B
{
    using namespace A;
    int i = 11;

    int k = i; // finds B::i, no ambiguity
}

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.

对我来说,标准说得很清楚,为了不合格的名称查找(int k = i 中的 i),考虑 A 中的 i 声明B 的成员,所以 iint k = i 中应该是不明确的,但是 gccclang compile and resolve i to the local B::i. I have searched the standard (basic.scope.hiding and namespace.udir) 并且没有发现异常或与上述规则相矛盾的规则。我发现对于合格的名称查找,但对于不合格的名称查找则不然。

为什么i是明确的?

关键是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."

指定的命名空间在A,using指令在B,最小的(实际上只是)公共命名空间是全局命名空间。因此 i 看起来就像在全局命名空间中声明的一样,并被 B::i.

隐藏