Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the compiler match "char" to "int" but not "short"?

I've got a small program:

#include<iostream>
using namespace std;

void f(int)   { cout << "int\n";   }
void f(short) { cout << "short\n"; }

int main(void){
    char c = 0;
    f(c);
    return 0;
}

It prints int. I felt that, if this is because of "Integer promotion", why is not short preferred?

I also know that integer promotion happens in an expression (like A=B). But I don't have expression in call to f(), right?

If this is related to overload resolution rule, why passing char to f will result into compilers preferring int to short?

If I delete f(int), then f(c) will call f(short)!

So in summary, my question is, is it related to "Integer promotion" or just "overload resolution rule"? And why?

like image 494
Hind Forsum Avatar asked May 11 '16 14:05

Hind Forsum


2 Answers

(Integral) Promotion is preferred to other (integral) conversions by overload resolution

Ranking of implicit conversion sequences

1) Exact match: no conversion required, lvalue-to-rvalue conversion, qualification conversion, function pointer conversion, (since C++17) user-defined conversion of class type to the same class

2) Promotion: integral promotion, floating-point promotion

3) Conversion: integral conversion, floating-point conversion, floating-integral conversion, pointer conversion, pointer-to-member conversion, boolean conversion, user-defined conversion of a derived class to its base

So, the promotion from char to int is preferred over conversion from char to short.


What is promotion? you may ask. It is a special kind of conversion described by the standard.

Why is char to short not a promotion?, you may continue. Integral promotion is always to int or a larger type. There are no promotions to short.

The following implicit conversions are classified as integral promotions:

  • signed char or signed short can be converted to int;

  • unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise;

  • char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned char (see above);

  • wchar_t, char16_t, and char32_t can be converted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, unsigned long long; an unscoped enumeration type whose underlying type is not fixed can be converted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, or unsigned long long. If the value range is greater, no integral promotions apply;

  • an unscoped enumeration type whose underlying type is fixed can be converted to its promoted underlying type;

    (since C++11)

  • a bit field type can be converted to int if it can represent entire value range of the bit field, otherwise to unsigned int if it can represent entire value range of the bit field, otherwise no integral promotions apply; the type bool can be converted to int with the value false becoming ​0​ and true becoming 1.


Standard references (current standard draft):

[over.ics.scs] § 3

[conv.prom] § 1

like image 176
eerorika Avatar answered Nov 08 '22 22:11

eerorika


From Implicit conversion (cppreference):

The following implicit conversions are classified as integral promotions:

  • [...]
  • char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned 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.

like image 23
Rakete1111 Avatar answered Nov 08 '22 23:11

Rakete1111