I\'ve got a small program:
#include
using namespace std;
void f(int) { cout << \"int\\n\"; }
void f(short) { cout << \"short\\n\
From Implicit conversion (cppreference):
The following implicit conversions are classified as integral promotions:
- [...]
char
can be converted toint
orunsigned int
depending on the underlying type:signed char
orunsigned char
(see above);- [...]
So, if there is a function f(int)
and f(short)
, the compiler will try to do an integer promotion first, if it is not possible, it will fallback to a integer conversion.
char
to int
is an integer promotion (see above), so the compiler will choose it.
If there isn't any f(int)
, the compiler will fail to find a function where it can do integer promotion, and will fallback to integer conversion. It finds a f(short)
, and a char
can be converted into a short
, so it will choose it.