using-declaration是否仅导入在using-declaration之上声明的重载?

HC4*_*ica 3 c++ scope overloading namespaces using-declaration

例如,GCC和clang都无法编译以下代码:

struct S {};

namespace N
{
    void g(S);
}

using N::g;

namespace N
{
    void g(int);
}

int main()
{
    g(0);
}
Run Code Online (Sandbox Code Playgroud)

有错误:

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

建议using声明仅导入在using声明出现点之上声明的重载,而不是稍后出现的(但在使用名称之前).

这种行为是否正确?

Alo*_*ave 6

这种行为是否正确?

是的,这种行为是正确的,并且根据c ++标准定义良好.

相关部分是C++ 11标准的第7.3.3.11节:

使用声明声明的实体应在上下文中根据其使用声明时的定义使用它.在使用名称时,不考虑在using-declaration之后添加到命名空间的定义.

[ 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 ]
Run Code Online (Sandbox Code Playgroud)