简体   繁体   中英

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

VS2015 compiles and executes the following snippet without a problem. g++ and clang don't link the code, and I think they are correct.

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

Your example is almost identical to the one in [basic.link]/7 - Yes, your interpretation is correct.
Using an undefined function q renders your program ill-formed NDR . Hence VC++ is technically conforming. However, you definitely want to report it.

Note how VC++ produces the same output ("q()") even if we add an inner definition of q :

namespace X {
    void p() {
        void q();                    
        q();
    }

    void q() { std::cout << "This would be right"; }
}

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

…but does have sensible behavior when extern is used .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM