19.6.  Standard Expression Conversions

[ fromfile: types.xml id: stdconversion ]

Abstract

This section discusses expression conversions, including implicit type conversions through promotion or demotion, and explicit casting through a variety of casting mechanisms.

Automatic Expression Conversion Rules for x op y

  1. Any bool, char, signed char, unsigned char, enum, short int, or unsigned short int is promoted to int. This is called an integral promotion.

  2. If, after the first step, the expression is of mixed type, then the operand of smaller type is promoted to that of the larger type, and the value of the expression has that type.

  3. The hierarchy of types is indicated by the arrows in Figure 19.1.

Figure 19.1. Hierarchy of Basic Types

Hierarchy of Basic Types

      double d;
      int i;
    

Example 19.8. src/types/mixed/mixed-types.cpp

#include <iostream>
using namespace std;

int main() {
    int i, j = 88;
    double d = 12314.8723497;
    cout << "initially d = " << d
         << "  and j = " << j << endl;
    cout << "The sum is: " << j + d << endl;
    i = d;
    cout << "after demoting d,  i = " << i << endl;
    d = j;
    cout << "after promoting j,  d = " << d << endl;
}

Here is the compile and run.

      src> g++ mixed-types.cpp
      mixed-types.cpp: In function `int main()':
      mixed-types.cpp:10: warning: converting to `int' from `double'
      src> ./a.out
      initially d = 12314.9  and j = 88
      The sum is: 12402.9
      after demoting d,  i = 12314
      after promoting j,  d = 88
      src>
    

Example 19.9. src/types/convert2bool.cpp

#include <iostream>
using namespace std;

int main() {
   int j(5);
   int* ip(&j);
   int* kp(0);
   double y(3.4);
   if(y)
      cout << "y looks like true to me!" << endl;
   else
      cout << "y looks like false to me!" << endl;
   cout << "ip looks like " << (ip ? "true" : "false") << endl;
   cout << "kp looks like " << (kp ? "true" : "false") << endl;
   while(--j) {
      cout << j << '\t';
   }
   cout << endl;
}


      src> ./a.out
      y looks like true to me!
      ip looks like true
      kp looks like false
      4       3       2       1
      src>