c++overload-resolution

Priority confusion on function overloading with multiple parameters


I'm learning C++ at school. We are currently on the topic of overloading functions. We were given an exercice with a intial code block. Then, we are shown a function call, we have to determine which function is choosen, or if the call is ambiguous (if it is). We use C++20.

This is the code block,

void fct (      int,          float)  {cout << "Function no 1" << endl;}
void fct (      int,          double) {cout << "Function no 2" << endl;}
void fct (      float&,       int&)   {cout << "Function no 3" << endl;}
void fct (const float&, const int&)   {cout << "Function no 4" << endl;}

      int       i = 1;
      int&     ri = i;
      char      c = 'A';
      float     f = 2.f;
      double    d = 3.0;
const double& rdC = d;

There are two functions calls that give me problems.

Call A: fct(f, 1);

For call A, our logic was :

Since function 4 is callable for both parameters, our conclusion is that function 4 is called, and it turns out that this what happens. Yay!

Call B: fct(f, i);

For call B, our logic was :

Since two functions are callable and share the same priority level for both parameters, we conclude that the call is ambiguous. BUT, it turns out that it's not, and that function 3 is called. And this, we do not understand.

We then assumed that for parameter (f), float& has highest priority than const float&, but then call A should be ambiguous (and it's not).

tl; dr

Why?


Solution

  • Firstly, note that two things can make a difference in overload resolution:

    fct(f, 1)

    Therefore, no. 4 is the best candidate.

    fct(f, i)

    Therefore, no. 3 is the best candidate.