-->

implicit conversion sequence in function overloadi

2019-07-13 11:26发布

问题:

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?