Search code examples
c++terminologyoverload-resolution

What is the compiler rank in argument type conversion if the parameters differ?


I am wondering about the rank during overload resolution.

I am reading through C++ Primer Fifth Edition. On page 245 they state the following:

6.6.1 Argument Type Conversions

  1. An exact match. An exact match happens when:
    • The argument and parameter types are identical.
    • The argument is converted from an array or function type to the corresponding pointer type.
    • A top-level const is added to or discarded from the argument.
  2. Match through a const conversion.
  3. Match through a promotion.
  4. Match through an arithmetic or pointer conversion.
  5. Match through a class-type conversion.

This makes sense to me. In the exercises to the section (6.52) the ask the following:

Given the following declarations,

void manip(int, int);
double dobj;

what is the rank of each conversion in the following calls?

(a) manip('a', 'z');    (b) manip(55.4, dobj);

This seemed pretty straightforward. I figured this is integral promotion on both parameters.. So the rank is therefore 3.

When trying to figure out (b), I come to the same conclusion.

Now this is all well and fine if the parameters are from the same type. But what about this?

void manip(double, const int& x) {

}

When I call the function like this: manip(2, 1); What is the common consensus for the rank in this circumstance?

There is a match through a const conversion (2) as well as a match through a promotion (3). So is the rank 2.5 then? This doesn't seem so obvious to me. I'm only interested in the terminological answer here. I know this is not really relevant by any means but I'm still interested. Do I misunderstand rank here?

On this github page they didn't argue with numbers either. What is the rank then in the common sense? Thanks for help in advance.


Solution

  • This seemed pretty straightforward. I figured this is integral promotion on both parameters.. So the rank is therefore 3.

    There are no mean/whole rank. Rank is done by parameter.

    So for both (a) and (b) each of the 2 parameters are integral promotion. You might note (3, 3) not 3.

    So in case of (3, 2), it is not 2.5 but (3, 2)

    Overload resolution takes the overload which has less or equal rank for each of its parameter.

    so

    • (1, 1, 2) would be better than (1, 2, 3)
    • (1, 1, 3) is not better than (3, 3, 1) (which is not better either). Here call would be ambiguous (if no other best overload).