]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example10.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)
10 #include <boost/safe_numerics/safe_integer.hpp>
13 using namespace boost::safe_numerics
;
15 void f(const unsigned int & x
, const int8_t & y
){
16 cout
<< x
* y
<< endl
;
19 const safe
<unsigned int> & x
,
20 const safe
<int8_t> & y
22 cout
<< x
* y
<< endl
;
26 cout
<< "example 4: ";
27 cout
<< "mixing types produces surprising results" << endl
;
29 std::cout
<< "Not using safe numerics" << std::endl
;
30 // problem: mixing types produces surprising results.
31 f(100, 100); // works as expected
32 f(100, -100); // wrong result - unnoticed
33 cout
<< "error NOT detected!" << endl
;;
35 catch(const std::exception
& e
){
37 cout
<< "error detected:" << e
.what() << endl
;;
40 // solution: use safe types
41 std::cout
<< "Using safe numerics" << std::endl
;
42 safe_f(100, 100); // works as expected
43 safe_f(100, -100); // throw error
44 cout
<< "error NOT detected!" << endl
;;
46 catch(const std::exception
& e
){
47 cout
<< "error detected:" << e
.what() << endl
;;