1 // Copyright (c) 2018 Robert Ramey 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See 4 // accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <iostream> 8 9 #include <boost/safe_numerics/checked_result.hpp> 10 #include <boost/safe_numerics/checked_result_operations.hpp> 11 main()12int main(){ 13 using ext_uint = boost::safe_numerics::checked_result<unsigned int>; 14 const ext_uint x{4}; 15 const ext_uint y{3}; 16 17 // operation is a success! 18 std::cout << "success! x - y = " << x - y; 19 20 // subtraction would result in -1, and invalid result for an unsigned value 21 std::cout << "problem: y - x = " << y - x; 22 23 const ext_uint z = y - x; 24 std::cout << "z = " << z; 25 // sum of two negative overflows is a negative overflow. 26 std::cout << "z + z" << z + z; 27 28 return 0; 29 } 30