显然,模糊调用不会导致GCC上的编译错误 [英] Obviously ambiguous call does not cause a compilation error on GCC

查看:171
本文介绍了显然,模糊调用不会导致GCC上的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到惊讶的是,GCC 认为在以下程序中对 foo()的调用不明确:

I was surprised by the fact that GCC does not consider the call to foo() in the following program ambiguous:

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

上述函数调用编译并返回 true 在GCC 4.7.2和GCC 4.8.0(测试版),而它不会编译(如我所料)在Clang 3.2和ICC 13.0.1。

The above function call compiles and returns true on GCC 4.7.2 and GCC 4.8.0 (beta), while it won't compile (as I would expect) on Clang 3.2 and ICC 13.0.1.

这是无需诊断的情况,或者是GCC中的错误? +11标准。

Is this a case of "no diagnostic required", or is it a bug in GCC? References to the C++11 Standard are encouraged.

推荐答案

§7.3.3/ 3:


在用作成员声明的using声明中,嵌套名称说明符应命名正在定义的类的基类。如果这样的using-declaration命名一个构造函数,那么嵌套名称说明符应该命名被定义类的直接基类;否则它会引入成员名称查找(10.2,3.4.3.1)找到的一组声明。

¶14:


... [注意:两个using声明可能引入具有相同名称和相同参数类型的函数。如果对一个不合格函数名的调用,函数重载解析选择了这种使用声明引入的函数,那么函数调用就会失败。

… [ Note: Two using-declarations may introduce functions with the same name and the same parameter types. If, for a call to an unqualified function name, function overload resolution selects the functions introduced by such using-declarations, the function call is ill-formed.

¶16:


为了重载解析,使用声明引入的函数b $ b派生类将被视为是派生类的成员。

For the purpose of overload resolution, the functions which are introduced by a using-declaration into a derived class will be treated as though they were members of the derived class.

因此,使用声明是合法的,但是函数是在同一重载集合中的同行,如你所说,程序是不成形的。

So, the using declarations are legal, but the functions are peers in the same overload set, as you said, and the program is ill-formed.

这篇关于显然,模糊调用不会导致GCC上的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆