Search code examples
c++c++14linkage

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

Solution

  • 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.