Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

linkage

c++14

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();
}
like image 481
Belloc Avatar asked Sep 26 '22 08:09

Belloc


1 Answers

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.

like image 171
Columbo Avatar answered Nov 15 '22 09:11

Columbo