Livace
Livace

Reputation: 126

How does implicit conversion work in C++

I'm trying to figure out how implicit conversion works.
I have 3 functions

Foo(int, int)
Foo(short, short)
Foo(short, unsigned char)

Then I call

Foo(unsigned char, unsigned char)

and Foo(int, int) is called. Can somebody explain how it works?

Upvotes: 6

Views: 311

Answers (1)

Holt
Holt

Reputation: 37606

TL;DR; This is ill-formed according to the standard, which is why you get warnings/errors when compiling with gcc/clang. There is an ambiguity between the first and the third call because the implicit conversion sequence from unsigned char to short is worse than the one from unsigned char to int (first one is a conversion while the second one is a promotion).


You have to consider 3 different implicit conversion sequences in this example, each one containing a single standard conversion:

  • unsigned char to unsigned char, which is an exact match.
  • unsigned char to short which is an integral conversion.
  • unsigned char to int which is an integral promotion.

The ranking of the conversion sequences is a follows:

exact match > integral promotion > integral conversion

For an overload FooA to be better than an overload FooB, implicit conversions for all arguments of FooA have to be not worse than implicit conversions for arguments of FooB, and at least one of the implicit conversion for FooA must be better than the corresponding conversion for FooB.

In your case:

  • Foo(int, int) vs. Foo(short, short) — First one is better because unsigned char to int (promotion) is better than unsigned char to short (conversion).
  • Foo(int, int) vs. Foo(short, unsigned char) — This is ambiguous:
    • unsigned char to int is better than unsigned char to short.
    • unsigned char to int is worse than unsigned char to unsigned char.
  • Foo(short, short) vs. Foo(short, unsigned char) — Second one is better because unsigned char to unsigned char (exact match) is better than unsigned char to short (conversion).

There is an ambiguity only between Foo(int, int) and Foo(short, unsigned char), the second overload Foo(short, short) does not participate in the "ambiguity". If you remove either the first or the third overload, the ambiguity disappears and the first or the third overload is chosen.

Upvotes: 10

Related Questions