Does the declaration of use use only import overloads declared above declarations?

For example, GCC and clang both cannot compile the following code:

struct S {};

namespace N
{
    void g(S);
}

using N::g;

namespace N
{
    void g(int);
}

int main()
{
    g(0);
}

with an error:

test.cpp: In function 'int main()':
test.cpp:17:8: error: could not convert '0' from 'int' to 'S'
     g(0);
        ^

suggesting that the use-declaration only import the overloaded points declared above at which the declaration of use appears, and not those that may appear later (but before the name is used).

Is this behavior right?

+3
source share
1 answer

Is this behavior right?

Yes, this is the correct behavior and is well defined according to the C ++ standard.

The relevant section of Β§ 7.3.3.11 of the C ++ 11 standard:

, -, , -. , -, .

[ Example:    
    namespace A {
        void f(int);
     }
    using A::f; // f is a synonym for A::f;
    // that is, for A::f(int).
    namespace A {
        void f(char);
    }
    void foo() {
        f(’a’); // calls f(int),
    } // even though f(char) exists.
    void bar() {
        using A::f; // f is a synonym for A::f;
        // that is, for A::f(int) and A::f(char).
        f(’a’); // calls f(char)
    }
 β€”end example ]
+6

All Articles