C ++ overload ambiguity: conversion and promotion by 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 is ambiguity. Check this! . However, the second argument is a signed integer. When binding a parameter intto a parameter long int, advancement is required, but to float, conversion.

Since the first argument is an exact match for both overloads, it is not taken into account. But with respect to the second parameter, its rank at the first overload (advance) is better than the rank of the second (transformation).

Why is there ambiguity in resolution and not choice of first overload?

+4
source share
3

int to long - . short int . (. [Conv.prom] .)

, float double . double to long double .

+8

5 int. , :

  • int to long int (aka long)
  • int to float

1) long int, .

2) int float - , " ":

Integer prvalue . , , .

+1

Because in any case there is no exact match. 5, without qualification, is of type int. Your methods declare a "float" or "long int" as an argument, and both require conversion. long! = int!

0
source

Source: https://habr.com/ru/post/1583772/


All Articles