c++ - 对函数的调用是不明确的,但为什么呢?

标签 c++ clang overloading ambiguity

#include <iostream>
using namespace std;

void x(int a,int b){
  cout<<"int int"<<endl;
}
void x(char a,char b){
  cout<<"char char"<<endl;
}

int main() {
  int a =2;char c ='a';
  x(a,c);
  return 0; 
}
在 apple clang 编译器中对“x”的调用不明确,为什么?
对于 x(int,int),第一个参数是直接匹配,第二个参数是促销
对于 x(char, char) 第一个参数是我知道的标准转换,也根据这个答案-> https://stackoverflow.com/a/28184631/13023201
并且提升应该优先于 std 转换,然后应该调用 x(int,int) 。那为什么会这么暧昧呢??

最佳答案

怎么看cppreference描述重载解析过程:

Best viable function

For each pair of viable function F1 and F2, the implicit conversion sequences from the i-th argument to i-th parameter are ranked to determine which one is better (except the first argument, the implicit object argument for static member functions has no effect on the ranking)

F1 is determined to be a better function than F2 if implicit conversions for all arguments of F1 are not worse than the implicit conversions for all arguments of F2, and

...

x(int, int)是比 x(char, char) 更好的匹配对于第一个参数,因为 intint是完全匹配和 intchar是一种转换。但是x(int, int)是第二个参数的较差匹配,因为它是提升而不是精确匹配。所以这两个函数都不比另一个更好,并且无法解决重载。

关于c++ - 对函数的调用是不明确的,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65263083/

相关文章:

c++ - 当两个候选人具有相同的简历资格时,转换函数的初始化是否应该不明确?

具有相同名称的 C++ const 和可变函数

linux - 查询 : Why am I getting a clang error on my OSX terminal?

c++ - 从 const 成员函数返回指向成员数组的指针

C++宏定义相互依赖的类

c++ - STL std::map 动态排序

c++ - CLang 3.5 LibTooling:在 clang::VarDecl 中获取变量的文件名

python - "' cc ' failed with exit status 1"安装python库时出错

c++ - 继承函数的重载解决方案

c++ - while 循环不工作 C++