Files
safe_numerics/examples/example84.cpp
Robert Ramey 29cf7f3d12 progress on example 93 to demonstrate trapping all potential exceptions at compile time
removed constexpr from exception policies.
  this eliminates obstacle to gcc compilation which doesn't support constexpr throw
  unfortunately, all versions of gcc trip compiler fault so gcc not supported for now
safe_literal - make this an unsafe type since it doesn't have policies - this might change in the future
2016-01-11 11:01:15 -08:00

53 lines
1.5 KiB
C++

#include <stdexcept>
#include <iostream>
#include "../include/safe_range.hpp"
#include "../include/safe_literal.hpp"
#include "../include/automatic.hpp"
#include "../include/exception.hpp"
#include "safe_format.hpp" // prints out range and value of any type
using namespace boost::numeric; // for safe_literal
using safe_t = safe_signed_range<
-24,
82,
automatic,
trap_exception
>;
// define variables use for input
using input_safe_t = safe_signed_range<
-24,
82,
automatic, // we don't need automatic in this case
throw_exception // these variables need to
>;
// function arguments can never be outside of limits
auto f(const safe_t & x, const safe_t & y){
auto z = x + y; // we know that this cannot fail
std::cout << "z = " << safe_format(z) << std::endl;
std::cout << "(x + y) = " << safe_format(x + y) << std::endl;
std::cout << "(x - y) = " << safe_format(x - y) << std::endl;
return z;
}
int main(int argc, const char * argv[]){
std::cout << "example 84:\n";
try{
input_safe_t x, y;
std::cin >> x >> y; // read varibles, throw exception
std::cout << "x" << safe_format(x) << std::endl;
std::cout << "y" << safe_format(y) << std::endl;
std::cout << "z" << safe_format(f(x, y)) << std::endl;
}
catch(const std::exception & e){
// none of the above should trap. Mark failure if they do
std::cout << e.what() << std::endl;
return false;
}
return 0;
}