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

标签 c++ c++11 language-lawyer c++14 linkage

这段代码在 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/

相关文章:

c++ - OpenCV训练错误(人脸识别)

c++ - OpenCV findContours 堆栈溢出

c++ - emplace() 的参数转发使构造函数参数为 const

C++类中的C++过期机制

c++ - 列表初始化-C++ 14中发生了什么变化?

c++ - 虚拟机中的OpenGL问题

c++ - Understanding floating point variables and operators in c++(也可能是书上的错误)

c++ - 未调用 move 赋值运算符

c++ - 围绕函数调用解析的混淆

c++ - C++11 认为 "thread"是什么?