https://rentry.org/PPP2_p79

// Code derived from Stroustrup's PPP2 book
// § 3.9.1 Safe conversions
// -beginning on p 79

#include <iostream>

using std::cout;

int main()
{
  char c  = 'x';
  int  i1 = c;
  int  i2 = 'x';
  char c2 = i1;

  cout << c << ' ' << i1 << ' ' << c2 << '\n';
  cout << i2 << '\n';

  double d1 = 2.3;
  double d2 = d1 + 2;  // 2 is converted to 2.0 before adding

  if (d1 < 0)  // 0 is converted to 0.0 before comparison
    cout << "d1 is negative";

  cout << d1 << '\n'  //
       << d2 << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_03/main_p79.cpp && ./a.out

PrevUpNext

Edit
Pub: 21 Jan 2023 06:08 UTC
Edit: 29 Apr 2023 06:33 UTC
Views: 662