]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example10.cpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / libs / safe_numerics / example / example10.cpp
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 #include <cstdint>
9
10 #include <boost/safe_numerics/safe_integer.hpp>
11
12 using namespace std;
13 using namespace boost::safe_numerics;
14
15 void f(const unsigned int & x, const int8_t & y){
16 cout << x * y << endl;
17 }
18 void safe_f(
19 const safe<unsigned int> & x,
20 const safe<int8_t> & y
21 ){
22 cout << x * y << endl;
23 }
24
25 int main(){
26 cout << "example 4: ";
27 cout << "mixing types produces surprising results" << endl;
28 try {
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;;
34 }
35 catch(const std::exception & e){
36 // never arrive here
37 cout << "error detected:" << e.what() << endl;;
38 }
39 try {
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;;
45 }
46 catch(const std::exception & e){
47 cout << "error detected:" << e.what() << endl;;
48 }
49 return 0;
50 }
51