Per*_*-lk 4 c++ overloading type-conversion type-promotion
在这段代码中:
void f(float f, long int i) { cout << "1" << endl; }
void f(float f, float d) { cout << "2" << endl; }
int main() {
f(5.0f, 5);
}
Run Code Online (Sandbox Code Playgroud)
有一种模棱两可的态度.看看这个!.但是,第二个参数是有符号整数.绑定int到long int参数需要促销,但要float转换.
由于第一个参数是两个重载的完全匹配,因此它不计算在内.但是关于第二个参数,它在第一个超载(促销)上的排名优于第二个参数(转换)上的排名.
为什么解决方案存在歧义,而不是选择第一次重载?
int到long是一个转换.short以int正在促销.(有关整体促销的完整列表,请参阅[conv.prom].)
同样,floatto double是浮点促销.double到long double是一个转换.