After trying to determine in the standard why one overload is preferable over another, I came to the conclusion that it should not, overload does not have a better conversion sequence than the other in the case of f( int1, float2 ) and the code should not be compiled. If your compiler accepts it, there may be an implementation error.
On the second question, the standard defines transformations that can be used to correspond to a function call, and also determines the rank for those transformations that serve as partial ordering (called a better transformation than). The compiler will always choose the best conversion, and if there is no better conversion, the program will be poorly formed, as in the example.
Code ambiguity checking with different compilers:
GCC:
conv.cpp:22: error: call of overloaded 'f(int&, float&)' is ambiguous conv.cpp:5: note: candidates are: float f(int, int) conv.cpp:9: note: float f(float, float)
clank:
conv.cpp:22:13: error: call to 'f' is ambiguous cout << f(int1, float2) << endl; // output: 2 ^ conv.cpp:5:7: note: candidate function float f (int a, int b) { ^ conv.cpp:9:7: note: candidate function float f (float a, float b) { ^
MSVS 2010 (thanks to Xeo):
error C2666: 'f' : 2 overloads have similar conversions src\main.cpp(2): could be 'void f(int,int)' src\main.cpp(1): or 'void f(float,float)' while trying to match the argument list '(int, float)'
source share