Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

call to 'x' is ambiguous in apple clang compiler, why?

for x(int,int), first argument is a direct match and second is a promotion for x(char, char) first argument is a standard conversion as I know and also according to this answer-> https://stackoverflow.com/a/28184631/13023201

And promotion should be preferred over std conversion, then x(int,int) should be called. Then why is this ambiguous??

like image 793
Robert Page Avatar asked Jan 25 '23 14:01

Robert Page


1 Answers

Looking at how cppreference describes the overload resolution process:

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) is a better match than x(char, char) for the first argument, since int to int is an exact match and int to char is a conversion. But x(int, int) is a worse match for the second argument, because it's a promotion instead of an exact match. So neither function is a better match than the other and the overload cannot be resolved.

like image 110
Nathan Pierson Avatar answered Jan 30 '23 12:01

Nathan Pierson