1 #ifndef BOOST_NUMERIC_NATIVE_HPP
2 #define BOOST_NUMERIC_NATIVE_HPP
4 // Copyright (c) 2012 Robert Ramey
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <type_traits>
13 // policy which creates results types and values equal to that of C++ promotions.
14 // When used in conjunction with a desired exception policy, traps errors but
15 // does not otherwise alter the results produced by the program using it.
17 namespace safe_numerics {
21 // arithmetic operators
22 template<typename T, typename U>
23 struct addition_result {
24 using type = decltype(
25 typename base_type<T>::type()
26 + typename base_type<U>::type()
29 template<typename T, typename U>
30 struct subtraction_result {
31 using type = decltype(
32 typename base_type<T>::type()
33 - typename base_type<U>::type()
36 template<typename T, typename U>
37 struct multiplication_result {
38 using type = decltype(
39 typename base_type<T>::type()
40 * typename base_type<U>::type()
43 template<typename T, typename U>
44 struct division_result {
45 using type = decltype(
46 typename base_type<T>::type()
47 / typename base_type<U>::type()
50 template<typename T, typename U>
51 struct modulus_result {
52 using type = decltype(
53 typename base_type<T>::type()
54 % typename base_type<U>::type()
57 // note: comparison_result (<, >, ...) is special.
58 // The return value is always a bool. The type returned here is
59 // the intermediate type applied to make the values comparable.
60 template<typename T, typename U>
61 struct comparison_result {
62 using type = decltype(
63 typename base_type<T>::type()
64 + typename base_type<U>::type()
69 template<typename T, typename U>
70 struct left_shift_result {
71 using type = decltype(
72 typename base_type<T>::type()
73 << typename base_type<U>::type()
76 template<typename T, typename U>
77 struct right_shift_result {
78 using type = decltype(
79 typename base_type<T>::type()
80 >> typename base_type<U>::type()
84 template<typename T, typename U>
85 struct bitwise_or_result {
86 using type = decltype(
87 typename base_type<T>::type()
88 | typename base_type<U>::type()
91 template<typename T, typename U>
92 struct bitwise_and_result {
93 using type = decltype(
94 typename base_type<T>::type()
95 & typename base_type<U>::type()
98 template<typename T, typename U>
99 struct bitwise_xor_result {
100 using type = decltype(
101 typename base_type<T>::type()
102 ^ typename base_type<U>::type()
110 #endif // BOOST_NUMERIC_NATIVE_HPP