]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example4.cpp
1 // Copyright (c) 2018 Robert Ramey
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)
9 #include <boost/safe_numerics/safe_integer.hpp>
12 std::cout
<< "example 4: ";
13 std::cout
<< "implicit conversions change data values" << std::endl
;
14 std::cout
<< "Not using safe numerics" << std::endl
;
16 // problem: implicit conversions change data values
20 std::cout
<< "a is " << a
<< " b is " << b
<< '\n';
22 std::cout
<< "a is less than b\n";
25 std::cout
<< "b is less than a\n";
27 std::cout
<< "error NOT detected!" << std::endl
;
29 catch(const std::exception
&){
30 // never arrive here - just produce the wrong answer!
31 std::cout
<< "error detected!" << std::endl
;
35 // solution: replace int with safe<int> and unsigned int with safe<unsigned int>
36 std::cout
<< "Using safe numerics" << std::endl
;
38 using namespace boost::safe_numerics
;
39 safe
<signed int> a
{-1};
40 safe
<unsigned int> b
{1};
41 std::cout
<< "a is " << a
<< " b is " << b
<< '\n';
43 std::cout
<< "a is less than b\n";
46 std::cout
<< "b is less than a\n";
48 std::cout
<< "error NOT detected!" << std::endl
;
51 catch(const std::exception
& e
){
52 // never arrive here - just produce the correct answer!
53 std::cout
<< e
.what() << std::endl
;
54 std::cout
<< "error detected!" << std::endl
;