7

Section 4.5 of the C++ standard (integer promotion) talks about specific cases of converting integral types to types with a higher rank.

Section 4.7 of the C++ standard (integral conversions) begins with (bullet 4.7.1):

An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type.

As far as I understand conversions described in 4.5 (maybe except for the bullet 4.5.3 (enums)) can be performed by using the techniques from 4.7 section alone: 4.5.1 and 4.5.2 are completely covered by 4.7.1; 4.5.4 is covered by 4.7.4. So what's the purpose of the entire 4.5 section? What additional conversions does it enable? Maybe I'm missing some restrictions?

P.S. I'm reading C++03 version of the standard.

2 Answers2

10

I think that the distinction is important because both do not fall in the same conversion category and have different rank (see 13.3.3.1.1, Standard conversion sequences). The rank makes a difference when it comes to overload resolution :

Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion.

In the end, I believe it is the distinction between 4.5 and 4.7 that makes the following code unambiguous :

#include <iostream>

void foo(int i)            { std::cout << "foo(int)" << std::endl; }
void foo(unsigned short i) { std::cout << "foo(unsigned short)" << std::endl; }

int main()
{
    foo(static_cast<short>(1));
}
  • short to int is a promotion (thus having promotion rank)
  • short to unsigned short is a conversion (thus having conversion rank)

In the end, this code calls foo(int) because it is a better candidate.

icecrime
  • 74,451
  • 13
  • 99
  • 111
5

Promotions occur during arithmetic and other operations. Conversions occur when merely storing one integral type inside another.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Hmm. So if I have `unsigned char a; signed char b; ... short c=a+b`, at the beginning `a` and `b` are *promoted* to (un)signed ints, added and then the result is *converted* to `short`? –  Jan 09 '11 at 17:36
  • 2
    @buratinas: a is promoted to unsingned int, b is promoted to signed int then result of promotion of b is converted to unsigned int then these are added and result is converted to signed short. – Öö Tiib Jan 09 '11 at 17:45
  • @Öö Tiib `a` is probably promoted to `int` (e.g. if you say `short c=a-b`, promotion to `unsigned` would give incorrect result) - unless `char` and `int` are of the same size – anatolyg Jan 09 '11 at 18:14
  • 2
    There's a point in there, though, that arithmetic ops can cause conversions as well as promotions, in order to coerce the types together. For another example: `unsigned int u = 2; int i = 2; u + i;`. `i` is *converted* (not promoted) to `unsigned`. – Steve Jessop Jan 09 '11 at 18:28
  • @SteveJessop true, this conversion is based on the _usual arithmetic conversions_ from **[expr]** but not from **[conv.integral]** (that the OP is quoting from: C++03, §4.7); it uses the terms _source type_ and _destination type_ which supports this answer. So integral conversions only occur when storing one integral type in another, which is not to be confused with usual arithmetic conversions. – legends2k Mar 11 '15 at 09:43