Files
safe_numerics/examples/example1.cpp
Robert Ramey d33389c450 converted test_conversion to
test_construction
test_assignment

pending - make m_t private
eliminate redundant bounds checking
2015-06-28 21:24:04 -07:00

50 lines
1.5 KiB
C++

#include <cassert>
#include <exception>
#include <iostream>
#include "../include/safe_integer.hpp"
void detected_msg(bool detected){
std::cout << (detected ? "error detected!" : "error NOT detected! ") << std::endl;
}
int main(int argc, const char * argv[]){
std::cout << "example 1:";
std::cout << "undetected erroneous expression evaluation" << std::endl;
std::cout << "Not using safe numerics" << std::endl;
try{
char x = 127;
char y = 2;
char z;
// this produces an invalid result !
z = x + y;
// it is the wrong result !!!
assert(z != 129);
// but assert fails to detect it since C++ implicitly
// converts variables to int before evaluating he expression!
assert(z != x + y);
std::cout << static_cast<int>(z) << " != " << x + y << std::endl;
detected_msg(false);
}
catch(std::exception){
assert(false); // never arrive here
}
// solution: replace char with safe<char>
std::cout << "Using safe numerics" << std::endl;
try{
using namespace boost::numeric;
safe<char> x = 127;
safe<char> y = 2;
safe<char> z;
// rather than producing and invalid result an exception is thrown
z = x + y;
assert(false); // never arrive here
}
catch(std::exception & e){
// which can catch here
std::cout << e.what() << std::endl;
detected_msg(true);
}
return 0;
}