And*_*owl 9 c++ diagnostics ambiguous-call language-lawyer c++11
我被这样的事实感到惊讶的是GCC并没有考虑调用foo()下面的程序含糊:
#include <iostream>
struct B1 { bool foo(bool) { return true; } };
struct B2 { bool foo(bool) { return false; } };
struct C : public B1, public B2
{
using B1::foo;
using B2::foo;
};
int main()
{
C c;
// Compiles and prints `true` on GCC 4.7.2 and GCC 4.8.0 (beta);
// does not compile on Clang 3.2 and ICC 13.0.1;
std::cout << std::boolalpha << c.foo(true);
}
Run Code Online (Sandbox Code Playgroud)
上面的函数调用true在GCC 4.7.2和GCC 4.8.0(beta)上编译并返回,而它不会在Clang 3.2和ICC 13.0.1上编译(如我所料).
这是" 无需诊断 "的情况,还是GCC中的错误?鼓励参考C++ 11标准.
\xc2\xa77.3.3/3:
\n\n\n\n\n在用作成员声明的 using 声明中,嵌套名称说明符应命名所定义的类的基类。如果这样的 using 声明命名了构造函数,则嵌套名称说明符应命名所定义的类的直接基类;否则它会引入通过成员名称查找找到的声明集 (10.2, 3.4.3.1)。
\n
\xc2\xb614:
\n\n\n\n\n\xe2\x80\xa6 [ 注意:两个 using 声明可能会引入具有相同名称和相同参数类型的函数。如果对于非限定函数名的调用,函数重载决议选择由此类 using 声明引入的函数,则该函数调用是格式错误的。
\n
\xc2\xb616:
\n\n\n\n\n出于重载决策的目的,通过 using 声明引入派生类的函数将被视为派生类的成员。
\n
因此,using声明是合法的,但正如您所说,这些函数是同一重载集中的对等体,并且该程序格式不正确。