]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/safe_numerics/example/example6.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / safe_numerics / example / example6.cpp
1 #include <stdexcept>
2 #include <sstream>
3 #include <iostream>
4
5 #include <boost/safe_numerics/safe_integer.hpp>
6
7 int main(int, const char *[]){
8 // problem: checking of externally produced value can be overlooked
9 std::cout << "example 6: ";
10 std::cout << "checking of externally produced value can be overlooked" << std::endl;
11 std::cout << "Not using safe numerics" << std::endl;
12
13 std::istringstream is("12317289372189 1231287389217389217893");
14
15 try{
16 int x, y;
17 is >> x >> y; // get integer values from the user
18 std::cout << x << ' ' << y << std::endl;
19 std::cout << "error NOT detected!" << std::endl;
20 }
21 catch(const std::exception &){
22 std::cout << "error detected!" << std::endl;
23 }
24
25 // solution: assign externally retrieved values to safe equivalents
26 std::cout << "Using safe numerics" << std::endl;
27 {
28 using namespace boost::safe_numerics;
29 safe<int> x, y;
30 is.seekg(0);
31 try{
32 is >> x >> y; // get integer values from the user
33 std::cout << x << ' ' << y << std::endl;
34 std::cout << "error NOT detected!" << std::endl;
35 }
36 catch(const std::exception & e){
37 std::cout << "error detected:" << e.what() << std::endl;
38 }
39 }
40 return 0;
41 }