如何解释 c++ 标准中的规则 [namespace.udir]p2?

How to interpret the rule [namespace.udir]p2 in the c++ standard?

我对 [namespace.udir]p2 的含义有点困惑。考虑以下程序:

namespace X { int i = 1; }

namespace Y { using namespace X; }

int main() { i = 2; }

在主程序中查找 i 的名称失败(我尝试使用 GCC、Clang 和 Visual C++)。这似乎与 [namespace.udir]p2 (http://eel.is/c++draft/dcl.dcl#namespace.udir-2):

不一致

A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup ([basic.lookup.unqual]), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]

在我的程序中,我将此规则应用于名称 i,由 int i = 1;X 中声明,方法如下:

这是否意味着 imain 中的非限定名称查找应该找到 X::i?为什么我尝试过的三个编译器都没有得到这个结果?

使用指令使名称可见在它们出现的范围内。例如,[basic.scope.namespace]p1

for each using-directive that nominates the member’s namespace, the member’s potential scope includes that portion of the potential scope of the using-directive that follows the member’s point of declaration

其中 名称的范围 是程序的一部分,可以通过 非限定查找.

同样,在[namespace.udir]p2中,

A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive.

这基本上与上面的引述相同。

在 OP 中,using-directive 出现在命名空间 Y 的命名空间范围内; main 超出该范围,因此 using-directive 对在 main.

内执行的名称查找没有影响