]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example10.cpp
4 #include <boost/safe_numerics/safe_integer.hpp>
7 using namespace boost::safe_numerics
;
9 void f(const unsigned int & x
, const int8_t & y
){
10 cout
<< x
* y
<< endl
;
13 const safe
<unsigned int> & x
,
14 const safe
<int8_t> & y
16 cout
<< x
* y
<< endl
;
20 cout
<< "example 4: ";
21 cout
<< "mixing types produces surprising results" << endl
;
23 std::cout
<< "Not using safe numerics" << std::endl
;
24 // problem: mixing types produces surprising results.
25 f(100, 100); // works as expected
26 f(100, -100); // wrong result - unnoticed
27 cout
<< "error NOT detected!" << endl
;;
29 catch(const std::exception
& e
){
31 cout
<< "error detected:" << e
.what() << endl
;;
34 // solution: use safe types
35 std::cout
<< "Using safe numerics" << std::endl
;
36 safe_f(100, 100); // works as expected
37 safe_f(100, -100); // throw error
38 cout
<< "error NOT detected!" << endl
;;
40 catch(const std::exception
& e
){
41 cout
<< "error detected:" << e
.what() << endl
;;