https://rentry.org/PPP2_p153b

// Code derived from Stroustrup's PPP2 book
// § 5.6.3 Bad input
//  -and beginning on p 153

#include <iostream>
#include <stdexcept>
#include <string>

using std::cerr;
using std::cout;
using std::exception;
using std::runtime_error;
using std::string;
using std::to_string;

void error(string s) { throw runtime_error(s); }

// run-time checked narrowing cast (type conversion).
template <class R, class A>
R narrow_cast(A const& a)
{
  R r = R(a);
  if (A(r) != a)
    error("info loss  " + to_string(a) + " -> " + to_string(r));

  return r;
}

int main()
try {
  int  x1 = narrow_cast<int>(2.9);    // throws
  int  x2 = narrow_cast<int>(2.0);    // OK
  char c1 = narrow_cast<char>(1066);  // throws
  char c2 = narrow_cast<char>(85);    // OK

  cout << x1 << '\n'  //
       << x2 << '\n'  //
       << c1 << '\n'  //
       << c2 << '\n';

} catch (exception& e) {
  cerr << "error: " << e.what() << '\n';
  // keep_window_open();
  return 1;

} catch (...) {
  cerr << "Oops: unknown exception!\n";
  // keep_window_open();
  return 2;
}

build & run:

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

PrevUpNext

Edit
Pub: 24 Feb 2023 09:45 UTC
Edit: 29 Apr 2023 09:45 UTC
Views: 527