From 1694d9482e6a9df2eca284034f1a6ec20d6912dc Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Wed, 25 Sep 2013 16:15:15 +0400 Subject: [PATCH] Fix example3 Examples 2 and 3 is the same. Change example 3 to show incorrect int to char conversion. --- safe_numerics/examples/example3.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/safe_numerics/examples/example3.cpp b/safe_numerics/examples/example3.cpp index 045bb99..d5d75b5 100644 --- a/safe_numerics/examples/example3.cpp +++ b/safe_numerics/examples/example3.cpp @@ -1,21 +1,20 @@ void example3(){ - // problem: undetected overflow in data type + // problem: implicit conversions change data values try{ - int x = INT_MAX; + int x = -1; // the following silently produces an incorrect result - ++x; - //std::cout << x << " != " << -1; + char y = x; detected_msg(false); } catch(...){ assert(false); // never arrive here } - // solution: replace int with safe + // solution: replace int with safe and char with safe try{ using namespace boost::numeric; - safe x = INT_MAX; - // throws exception when result is past maximum possible - ++x; + safe x = -1; + // throws exception when conversion change data value + safe y = x; assert(false); // never arrive here } catch(std::range_error & e){