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

Is my understanding about [basic.link]/7 in N4140 correct?

本文关键字:N4140 basic link 我对      更新时间:2023-10-16

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();
}

您的示例与[basic.link]/7中的示例几乎相同-是的,您的解释是正确的
使用未定义的函数q会导致程序格式不正确NDR。因此,VC++在技术上符合。然而,你肯定想报告它。

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

namespace X {
    void p() {
        void q();                    
        q();
    }
    void q() { std::cout << "This would be right"; }
}
void q() { std::cout << "q()" << 'n'; }

…但是当使用extern时确实具有可感知的行为。