这段代码在 clang 中编译,
namespace A {
void f() {
void g();
g();
}
}
void A::g() { }
但如果 g
在命名空间 A
中定义如下,GCC 只接受代码:
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.
GCC 是正确的。您的第一个片段格式不正确。
关于c++ - C++ 标准中与 [basic.link]/7 相关的 GCC 和 clang 之间的矛盾结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29928957/