mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-03 09:22:19 +00:00
test_construction test_assignment pending - make m_t private eliminate redundant bounds checking
38 lines
1.0 KiB
C++
38 lines
1.0 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 2:";
|
|
std::cout << "undetected overflow in data type" << std::endl;
|
|
try{
|
|
int x = INT_MAX;
|
|
// the following silently produces an incorrect result
|
|
++x;
|
|
std::cout << x << " != " << INT_MAX << " + 1" << std::endl;
|
|
detected_msg(false);
|
|
}
|
|
catch(std::exception){
|
|
assert(false); // never arrive here
|
|
}
|
|
// solution: replace int with safe<int>
|
|
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;
|
|
detected_msg(true);
|
|
}
|
|
return 0;
|
|
}
|