Why does constructor choose type INT instead of SHORT when invoked with a parameter of type CHAR?
The result of integral promotion is int
(not short
) for char
; and promotions (e.g. char
-> int
) have higher ranking than other conversions (e.g. char
-> short
) in overload resolution.
prvalues of small integral types (such as
char
) may be converted to prvalues of larger integral types (such asint
).
signed char
orsigned short
can be converted toint
;unsigned char
,char8_t
(since C++20) orunsigned short
can be converted toint
if it can hold its entire value range, andunsigned int
otherwise;char
can be converted toint
orunsigned int
depending on the underlying type:signed char
orunsigned char
(see above);
and (emphasis mine)
Note that all other conversions are not promotions; for example, overload resolution chooses
char
->int
(promotion) overchar
->short
(conversion).
The compiler always chooses the best matching overloading resolution.
in your case:
Type promotion is:
- A char, unsigned char or short can be promoted to an int. For example void f(int); can be a match for f('a');
- A float can be promoted to a double.
- A bool can be promoted to an int (FALSE counts as 0, TRUE as 1).
When casting implicitly, the compiler follows this ranking:
- Exact match
- Promotion
- Conversion
Since, char
to int
is integral promotion, it takes precedence over char
to short
which is conversion.
From here (emphasis mine):
char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned char