函数调用不明确,但为什么呢?

call to function is ambiguous, but why?

#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; 
}

调用 'x' 在 apple clang 编译器中不明确,为什么?

对于 x(int,int),第一个参数是直接匹配,第二个是提升 对于 x(char, char) 第一个参数是我所知道的标准转换,也根据这个答案->

并且提升应该优先于 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) 是第二个参数的 更差 匹配,因为它是一个提升而不是完全匹配。所以两个函数都比另一个更好,无法解决重载问题。