函数重载中的隐式转换序列 [英] implicit conversion sequence in function overloading

查看:105
本文介绍了函数重载中的隐式转换序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白编译器如何选择最佳候选者. 例如,让我们有以下代码:

I don't understand how the compiler chooses the best candidates. For example, let's have the following code:

int function(double, int, int){...}
int function(int, double, double){...}

如果第二个函数需要转换两个变量,而第一个函数只需要转换一个变量,那么为什么不选择第一个函数呢? 为什么这是一个模棱两可的情况?

If the second function needs to convert two variables and the first one has to only convert one variable, how come the first one isn't chosen? Why is this an ambiguous situation?

推荐答案

为什么这是模棱两可的情况?

Why is this an ambiguous situation?

根据§13.3.3/1

According to §13.3.3/1,

鉴于这些定义,可行的功能F1被定义为 更好的函数比另一个可行的函数F2 ,如果对于所有参数 i ,ICS i (F1)并不差转换顺序比 ICS i (F2),然后

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICS i (F1) is not a worse conversion sequence than ICS i (F2), and then

-对于某些参数 j ,ICS j (F1)是更好的转换 比ICS j (F2)的顺序,或者,如果不是这样,

— for some argument j, ICS j (F1) is a better conversion sequence than ICS j (F2), or, if not that,

— [...]

因此,像function(0., 0., 0.)这样的呼叫是模棱两可的;这两个重载都不比另一个重载更好.

Therefore a call like function(0., 0., 0.) is ambiguous; Neither of the overloads is a better match than the other.

考虑从函数调用中推导模板参数-如果在多个函数参数(例如T a, T b, T c)中使用了模板参数T,并且对于调用的两个参数,则推导为int,但对于第三个是double,是否真的可以成功地用T=int进行推演?

Consider template argument deduction from a function call - if a template parameter T is used in several function parameters (like T a, T b, T c) and for two of the arguments of the call it is deduced as int, but for the third one as double, should that really result in a successful deduction with T=int?

超载分辨率不会计算出更好的匹配,而会称为获胜者-这还不够果断.

Overload resolution doesn't count the better matches and calls the winner - that wouldn't be decisive enough.

想像一下拼图游戏-如果在两端填充得更好,而在另一端填充得不好,那么这真的是一个更好的缺口游戏吗?

Imagine a jigsaw puzzle - is a piece really a better match for a gap if it fills in better at two ends but worse on another one?

这篇关于函数重载中的隐式转换序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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