Search code examples
c++type-conversionoverloadingtype-promotion

C++ overload ambiguity: conversion versus promotion with primitive types


In this code:

void f(float f, long int i) { cout << "1" << endl; }
void f(float f, float d) { cout << "2" << endl; }

int main() {

   f(5.0f, 5);

}

there's an ambiguity. Check it out!. However, the second argument is a signed integer. Binding an int to a long int parameter requieres a promotion, but to float, a conversion.

Since the first argument is an exact match regarding both overloads, it doesn't count. But regarding the second parameter, its rank on the first overload (promotion) is better than the rank on the second (conversion).

Why is there a resolution ambiguity, instead of choosing the first overload?


Solution

  • int to long is a conversion. short to int is a promotion. (See [conv.prom] for the full list of integral promotions.)

    Similarly, float to double is the floating point promotion. double to long double is a conversion.