在 C++ 中使用 int 和 float 类型作为参数的函数重载

Function overloading using int and float type as an argument in c++

void sum(int x);
void sum(float x);

我不明白为什么这些函数重载语句会为 sum(2.3) 创建以下错误:

call of overloaded sum(double) is ambiguous

但是对于下面两个函数重载,效果很好。

void sum(int x);
void sum(double x);

2.3 是双精度数,编译器无法在转换为 int 或 float 之间进行选择(两者都会丢失精度)。