Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does implicit conversion work in C++

Tags:

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?

like image 867
Livace Avatar asked Dec 25 '17 11:12

Livace


People also ask

What is implicit conversion in C?

Overview. Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.

What happens in implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is implicit type conversion explain with example?

When an implicit conversion is done, it is not just a reinterpretation of the expression's value but a conversion of that value to an equivalent value in the new type. Consider the following example: float f = 3; // implicit conversion to float value 3.0 int i = 5.23f; // implicit conversion to integer value 5.


1 Answers

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.

like image 84
Holt Avatar answered Oct 03 '22 16:10

Holt