liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef BOOST_SAFE_NUMERICS_NATIVE_HPP
#define BOOST_SAFE_NUMERICS_NATIVE_HPP
 
//  Copyright (c) 2012 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 <type_traits>
#include <limits>
 
// policy which creates results types and values equal to that of C++ promotions.
// When used in conjunction with a desired exception policy, traps errors but
// does not otherwise alter the results produced by the program using it.
namespace boost {
namespace safe_numerics {
 
struct native {
public:
    // arithmetic operators
    template<typename T, typename U>
    struct addition_result {
        using type = decltype(
            typename base_type<T>::type()
            + typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct subtraction_result {
        using type = decltype(
            typename base_type<T>::type()
            - typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct multiplication_result {
        using type = decltype(
            typename base_type<T>::type()
            * typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct division_result {
        using type = decltype(
            typename base_type<T>::type()
            / typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct modulus_result {
        using type = decltype(
            typename base_type<T>::type()
            % typename base_type<U>::type()
        );
    };
    // note: comparison_result (<, >, ...) is special.
    // The return value is always a bool.  The type returned here is
    // the intermediate type applied to make the values comparable.
    template<typename T, typename U>
    struct comparison_result {
        using type = decltype(
            typename base_type<T>::type()
            + typename base_type<U>::type()
        );
    };
 
    // shift operators
    template<typename T, typename U>
    struct left_shift_result {
        using type = decltype(
            typename base_type<T>::type()
            << typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct right_shift_result {
        using type = decltype(
            typename base_type<T>::type()
            >> typename base_type<U>::type()
        );
    };
    // bitwise operators
    template<typename T, typename U>
    struct bitwise_or_result {
        using type = decltype(
            typename base_type<T>::type()
            | typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct bitwise_and_result {
        using type = decltype(
            typename base_type<T>::type()
            & typename base_type<U>::type()
        );
    };
    template<typename T, typename U>
    struct bitwise_xor_result {
        using type = decltype(
            typename base_type<T>::type()
            ^ typename base_type<U>::type()
        );
    };
};
 
} // safe_numerics
} // boost
 
#endif // BOOST_SAFE_NUMERICS_NATIVE_HPP