]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example8.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>
11 int main(int, const char *[]){
12 std::cout
<< "example 8:";
13 std::cout
<< "undetected erroneous expression evaluation" << std::endl
;
14 std::cout
<< "Not using safe numerics" << std::endl
;
19 // this produces an invalid result !
21 std::cout
<< "error NOT detected!" << std::endl
;
22 std::cout
<< z
<< " != " << y
<< " - " << x
<< std::endl
;
24 catch(const std::exception
&){
25 std::cout
<< "error detected!" << std::endl
;
27 // solution: replace int with safe<int>
28 std::cout
<< "Using safe numerics" << std::endl
;
30 using namespace boost::safe_numerics
;
31 safe
<unsigned int> x
= 127;
32 safe
<unsigned int> y
= 2;
34 // rather than producing an invalid result an exception is thrown
36 std::cout
<< "error NOT detected!" << std::endl
;
37 std::cout
<< z
<< " != " << y
<< " - " << x
<< std::endl
;
39 catch(const std::exception
& e
){
40 // which we can catch here
41 std::cout
<< "error detected:" << e
.what() << std::endl
;