Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contradictory results between GCC and clang related to [basic.link]/7 in the C++ Standard

This snippet compiles in clang,

namespace A {
    void f() {
        void g();
        g();
    }
}

void A::g() { }

but GCC only accepts the code if g is defined inside the namespace A as follows:

namespace A {
    void f() {
        void g();
        g();
    }
    void g() {}
}

But I believe there's nothing in [basic.link]/7 disallowing the first snippet above.

like image 579
Leon Avatar asked Apr 28 '15 20:04

Leon


1 Answers

[basic.link]/p7, emphasis mine:

When a block scope declaration of an entity with linkage is not found to refer to some other declaration, then that entity is a member of the innermost enclosing namespace. However such a declaration does not introduce the member name in its namespace scope.

[namespace.memdef]/p2, emphasis mine:

Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration’s namespace.

GCC is correct. Your first snippet is ill-formed.

like image 157
T.C. Avatar answered Sep 20 '22 12:09

T.C.