https://rentry.org/PPP2_p80

// Code derived from Stroustrup's PPP2 book
// § 3.9.2 Unsafe conversions
// -beginning on p 80

#include <iostream>

using std::cout;

int main()
{
  int  a = 20'000;
  char c = a;  // try to squeeze a large int into a small char
  int  b = c;

  if (a != b)  // != means “not equal”
    cout << "oops!:  " << a << " != " << b << '\n';
  else
    cout << "Wow! We have large characters\n";
}

build & run:

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

PrevUpNext

Edit
Pub: 21 Jan 2023 11:10 UTC
Edit: 29 Apr 2023 06:34 UTC
Views: 655