implicit conversion sequence in function overloading

你。 提交于 2019-12-12 15:07:49

问题


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?


回答1:


Why is this an ambiguous situation?

According to §13.3.3/1,

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

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

— [...]

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

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?



来源:https://stackoverflow.com/questions/25825867/implicit-conversion-sequence-in-function-overloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!