mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-25 04:32:14 +00:00
corrections adjustments motivated by first real review of the library
This commit is contained in:
@@ -3,36 +3,44 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "../include/safe_integer.hpp"
|
||||
//#include "../include/safe_compare.hpp"
|
||||
#include "../include/safe_compare.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 3: ";
|
||||
std::cout << "implicit conversions change data values" << std::endl;
|
||||
std::cout << "example 3:";
|
||||
std::cout << "undetected underflow in data type" << std::endl;
|
||||
std::cout << "Not using safe numerics" << std::endl;
|
||||
try{
|
||||
int x = -1000;
|
||||
unsigned int x = 0;
|
||||
// the following silently produces an incorrect result
|
||||
char y = x;
|
||||
--x;
|
||||
// because C/C++ implicitly converts mis-matched arguments to int
|
||||
// suggests that the operation is correct
|
||||
assert(x == -1);
|
||||
// even though it's not !!!
|
||||
|
||||
// however, safe_compare does detect the error
|
||||
assert(! boost::numeric::safe_compare::equal(x, -1));
|
||||
std::cout << x << " != " << -1;
|
||||
detected_msg(false);
|
||||
}
|
||||
catch(...){
|
||||
assert(false); // never arrive here
|
||||
}
|
||||
// solution: replace int with safe<int> and char with safe<char>
|
||||
// solution: replace unsigned int with safe<unsigned int>
|
||||
std::cout << "Using safe numerics" << std::endl;
|
||||
try{
|
||||
using namespace boost::numeric;
|
||||
safe<int> x = -1000;
|
||||
// throws exception when conversion change data value
|
||||
safe<char> y = x;
|
||||
safe<unsigned int> x = 0;
|
||||
// decrement unsigned to less than zero throws exception
|
||||
--x;
|
||||
assert(false); // never arrive here
|
||||
}
|
||||
catch(std::range_error & e){
|
||||
std::cout << e.what() << std::endl;
|
||||
std::cout << e.what();
|
||||
detected_msg(true);
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user