VS 2010:2 个重载具有相似的转换 [英] VS 2010 : 2 overloads have similar conversions

查看:77
本文介绍了VS 2010:2 个重载具有相似的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这里发生了什么

I don't understand what happens here

class A{};
class B : A {};

void func(A&, bool){}
void func(B&, double){}

int main(void)
{
    B b;
    A a;
    bool bo;
    double d;

    func(b, bo);
}

编译时,Visual 2010 在 func(b, bo);

When compiling, Visual 2010 gives me this error on line func(b, bo);

2 overloads have similar conversions
could be 'void func(B &,double)'
or       'void func(A &,bool)'
while trying to match the argument list '(B, bool)'

我不明白为什么 bool 参数不足以解决过载问题.我看过这个问题,正如接受的答案中所指出的, bool 应该更喜欢布尔过载.就我而言,我看到第一个参数不足以选择好的函数,但为什么第二个参数不能解决歧义?

I don't understand why the bool parameter isn't enough to resolve the overload. I've seen this question, and as pointed in the accepted answer, bool should prefer the bool overload. In my case, I see that first parameter isn't enough to choose the good function, but why the second parameter doesn't solve the ambiguity?

推荐答案

重载解析规则比 Pete Becker 写的还要复杂.对于 f 的每个重载,编译器不仅计算需要转换的参数数量,还计算转换的等级.

Overload resolution rules are even more complicated than Pete Becker wrote. For each overload of f, the compiler counts not only the number of parameters for which conversion is required, but the rank of conversion.

排名 1

无需转换

左值到右值的转换

数组到指针的转换

函数到指针的转换

资质转换

排名 2

整体促销

浮点促销

排名 3

积分转换

浮点数转换

浮点积分转换

指针转换

指向成员转换的指​​针

Pointer to member conversions

布尔转换

假设所有候选函数都是非模板函数,一个函数获胜当且仅当它有一个参数,该参数的排名优于其他候选函数中相同参数的排名,并且其他参数的排名不差.

Assuming that all candidates are non-template functions, a function wins if and only if it has a parameter which rank is better than the rank of the same parameter in other candidates and the ranks for the other parameters are not worse.

现在让我们来看看 OP 案例.

Now let's have a look at the OP case.

  • func(A&, bool):转换B&->A&(等级 3)对于第一个参数,第二个参数完全匹配(排名 1).
  • func(B&, double):第一个参数完全匹配(等级 1),第二个参数转换 bool->double(等级 3).
  • func(A&, bool): conversion B&->A& (rank 3) for the 1st parameter, exact match (rank 1) for the 2nd parameter.
  • func(B&, double): exact match (rank 1) for the 1st parameter, conversion bool->double (rank 3) for the 2nd parameter.

结论:没有人赢.

这篇关于VS 2010:2 个重载具有相似的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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