]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example10.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / safe_numerics / example / example10.cpp
1 #include <iostream>
2 #include <cstdint>
3
4 #include <boost/safe_numerics/safe_integer.hpp>
5
6 using namespace std;
7 using namespace boost::safe_numerics;
8
9 void f(const unsigned int & x, const int8_t & y){
10 cout << x * y << endl;
11 }
12 void safe_f(
13 const safe<unsigned int> & x,
14 const safe<int8_t> & y
15 ){
16 cout << x * y << endl;
17 }
18
19 int main(){
20 cout << "example 4: ";
21 cout << "mixing types produces surprising results" << endl;
22 try {
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;;
28 }
29 catch(const std::exception & e){
30 // never arrive here
31 cout << "error detected:" << e.what() << endl;;
32 }
33 try {
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;;
39 }
40 catch(const std::exception & e){
41 cout << "error detected:" << e.what() << endl;;
42 }
43 return 0;
44 }
45