GCC 和 clang 之间的矛盾结果与 C++ 标准中的 [basic.link]/7 相关

Contradictory results between GCC and clang related to [basic.link]/7 in the C++ Standard

这段代码在 clang 中编译,

namespace A {
    void f() {
        void g();
        g();
    }
}

void A::g() { }

但 GCC 仅在 g 定义在命名空间 A 内时才接受代码,如下所示:

namespace A {
    void f() {
        void g();
        g();
    }
    void g() {}
}

但我相信 [basic.link]/7 中没有任何内容禁止上面的第一个代码段。

[basic.link]/p7,强调我的:

When a block scope declaration of an entity with linkage is not found to refer to some other declaration, then that entity is a member of the innermost enclosing namespace. However such a declaration does not introduce the member name in its namespace scope.

[namespace.memdef]/p2,强调我的:

Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration’s namespace.

海湾合作委员会是正确的。您的第一个代码段格式错误。

[basic.link]/7

我觉得很清楚

...However such a declaration does not introduce the member name in its namespace scope.

clang 是错误的。同样,您不会期望它能够编译:

namespace A
{
}

void A::foo()
{
}