在最近的封闭范围内使用 using 指令的局部外部变量声明

Local extern variable declaration with using-directive in the nearest enclosing scope

这个程序是否符合 c++ 标准?

namespace X { int i = 1; }

using namespace X;

int main() {
    extern int i;
    i = 2;
}

我用不同的编译器得到不同的结果:

[basic.link]/p6:

If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage.

X::i 是在 extern 声明的最内层封闭命名空间(即全局命名空间)之外声明的,因此它会被忽略。这意味着没有找到 i 的声明,因此 extern int i 是一个名为 i 的新变量的声明,具有外部 linkage.

您的程序将编译,但如果使用块作用域 i,则不会 link。