c++ - 如何解决方法重载中的函数调用?

标签 c++

void add(int,int);
void add(int ,float);
void add(float,int);

unsigned int i = 10;
unsigned float j = 1.0;
add(i,f); // ambiguios call error

如果我从程序中删除 unsigned 那么它工作正常。

int i = 10;
float j = 1.0;
add(i,f); // working

为什么在重载函数中使用无符号变量会导致歧义调用

最佳答案

在 C++ 中没有称为 unsigned float 的东西。 float 总是signed

根据 §7.1.5.2 中的 C++ 标准表 7,“signed”本身就是“int”的同义词。 所以编译器应该给你一个错误,signedunsigned 不适用于 float

检查 here , 连 Ideone 都报错。

error: ‘signed’ or ‘unsigned’ invalid for ‘j’

您是否将此错误错误解释为 ambiguos 函数调用错误?

如果删除 unsigned float,编译器将看不到任何具有参数 unsigned intfloat 的匹配函数调用,因此它会提升unsigned intint 并使用参数 intfloat 解析对函数的调用,没有歧义。

Here是 Ideone 上的代码示例。

关于c++ - 如何解决方法重载中的函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381335/

相关文章:

OBJ-C 中的 C++ 图形等价物

c++ - 如何计算数组中出现的次数?

c++ - 将推力 vector 输入 getrf/getri 时出现问题

c++ - 如何在 codeblocks ide 的左侧显示 .cpp 和 .h 文件?

c++ - 为什么当我将 std::locale 设置为 "zh_CN.UTF-8"时 std::istringstream 失败?

c++ - 如何优雅地编写使用模板参数隐含值的类模板?

c++ - 如何实现 const_auto,因为 C++11 缺少它?

c++ - C/C++ 创建一个带有负值的枚举,而不必对其编号

c++ - 关于Makefile的一些问题

c++ - 请帮助我理解这个带有参数的 C++ 参数声明