#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 toi
-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)
更好的匹配对于第一个参数,因为 int
至 int
是完全匹配和 int
至 char
是一种转换。但是x(int, int)
是第二个参数的较差匹配,因为它是提升而不是精确匹配。所以这两个函数都不比另一个更好,并且无法解决重载。
关于c++ - 对函数的调用是不明确的,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65263083/