Files
safe_numerics/examples/example2.cpp
2015-12-07 21:46:39 -08:00

37 lines
1.1 KiB
C++

#include <cassert>
#include <exception>
#include <iostream>
#include "../include/safe_integer.hpp"
int main(int argc, const char * argv[]){
std::cout << "example 2:";
std::cout << "undetected overflow in data type" << std::endl;
// problem: undetected overflow
std::cout << "Not using safe numerics" << std::endl;
try{
int x = INT_MAX;
// the following silently produces an incorrect result
++x;
std::cout << x << " != " << INT_MAX << " + 1" << std::endl;
std::cout << "error NOT detected!" << std::endl;
}
catch(std::exception){
std::cout << "error detected!" << std::endl;
}
// solution: replace int with safe<int>
std::cout << "Using safe numerics" << std::endl;
try{
using namespace boost::numeric;
safe<int> x = INT_MAX;
// throws exception when result is past maximum possible
++x;
assert(false); // never arrive here
}
catch(std::exception & e){
std::cout << e.what() << std::endl;
std::cout << "error detected!" << std::endl;
}
return 0;
}