C++重载歧义:使用原始类型进行转换与提升

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)

有一种模棱两可的态度.看看这个!.但是,第二个参数是有符号整数.绑定intlong int参数需要促销,但要float转换.

由于第一个参数是两个重载的完全匹配,因此它不计算在内.但是关于第二个参数,它在第一个超载(促销)上的排名优于第二个参数(转换)上的排名.

为什么解决方案存在歧义,而不是选择第一次重载?

T.C*_*.C. 8

intlong是一个转换.shortint正在促销.(有关整体促销的完整列表,请参阅[conv.prom].)

同样,floatto double是浮点促销.doublelong double是一个转换.