4

I was studying function overloading in c++, and I saw an ambiguous condition, in the program,

long add(long a){
    long b = a;
    return b;
}

double add(double a){
    double b = a;
    return b;
}

int main(){
    int x;
    x = add(10);
    printf("x : %d", x);
    getch();
    return 0;   
}

The reason of ambiguity given in the book was, the compiler may convert int either in long or in double. So compiler generates an error. I run this and the result was same, error.

So how long and double are equivalent? what's the actual reason of ambiguity here?

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65

1 Answers1

3

It is not that long and double are equivalent in themselves, but rather the conversion rules int->long and int->double have equivalent priority, so the compiler faces an ambiguity in the presence of both options.

Smeeheey
  • 9,906
  • 23
  • 39