我对N4140中[basic.link]/7的理解是否正确?

Bel*_*loc 5 c++ linkage c++14

VS2015编译并执行以下代码段没有问题.g ++和clang没有链接代码,我认为它们是正确的.

#include <iostream>

namespace X {
    void p() {
        void q();   // This is a block scope declaration of the function q() with external
                    // linkage (by §3.5/6), which then must be defined in namespace X,
                    // according to §3.5/7, and not in the global namespace.
        q();
    }
}

void q() { std::cout << "q()" << '\n'; }

int main()
{
    X::p();
}
Run Code Online (Sandbox Code Playgroud)

Col*_*mbo 4

您的示例与 [basic.link]/7 中的示例几乎相同 - 是的,您的解释是正确的。
\n使用未定义的函数q会使您的程序出现格式错误的NDR。因此 VC++在技术上是符合要求的。但是,您肯定想举报。

\n\n

请注意 VC++ 如何产生相同的输出(“q()”),即使我们添加 的内部定义q

\n\n
namespace X {\n    void p() {\n        void q();                    \n        q();\n    }\n\n    void q() { std::cout << "This would be right"; }\n}\n\nvoid q() { std::cout << "q()" << \'\\n\'; }\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\xa6but在extern使用时确实具有明智的行为。

\n