summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/safe_numerics/example/example3.cpp
blob: 15b810ebb87538f19322f04bbdaefdec57b9adb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//  Copyright (c) 2018 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>

#include <boost/safe_numerics/safe_integer.hpp>

int main(int, const char *[]){
    std::cout << "example 3:";
    std::cout << "undetected underflow in data type" << std::endl;
    std::cout << "Not using safe numerics" << std::endl;
    // problem: decrement can yield incorrect result
    try{
        unsigned int x = 0;
        // the following silently produces an incorrect result
        --x;
        std::cout << x << " != " << -1 << std::endl;

        // when comparing int and unsigned int, C++ converts
        // the int to unsigned int so the following assertion
        // fails to detect the above error!
        assert(x == -1);

        std::cout << "error NOT detected!" << std::endl;
    }
    catch(const std::exception &){
        // never arrive here
        std::cout << "error detected!" << std::endl;
    }
    // solution: replace unsigned int with safe<unsigned int>
    std::cout << "Using safe numerics" << std::endl;
    try{
        using namespace boost::safe_numerics;
        safe<unsigned int> x = 0;
        // decrement unsigned to less than zero throws exception
        --x;
        assert(false); // never arrive here
    }
    catch(const std::exception & e){
        std::cout << e.what() << std::endl;
        std::cout << "error detected!" << std::endl;
    }
    return 0;
}