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

前端 未结 2 560
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:38

I\'ve got a small program:

#include
using namespace std;

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


        
相关标签:
2条回答
  • 2021-01-30 20:17

    (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

    0 讨论(0)
  • 2021-01-30 20:22

    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.

    0 讨论(0)
提交回复
热议问题