| 1 | #ifndef BOOST_SAFE_NUMERICS_NATIVE_HPP |
| 2 | #define BOOST_SAFE_NUMERICS_NATIVE_HPP |
| 3 | |
| 4 | // Copyright (c) 2012 Robert Ramey |
| 5 | // |
| 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) |
| 9 | |
| 10 | #include <type_traits> |
| 11 | #include <limits> |
| 12 | |
| 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. |
| 16 | namespace boost { |
| 17 | namespace safe_numerics { |
| 18 | |
| 19 | struct native { |
| 20 | public: |
| 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() |
| 27 | ); |
| 28 | }; |
| 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() |
| 34 | ); |
| 35 | }; |
| 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() |
| 41 | ); |
| 42 | }; |
| 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() |
| 48 | ); |
| 49 | }; |
| 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() |
| 55 | ); |
| 56 | }; |
| 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() |
| 65 | ); |
| 66 | }; |
| 67 | |
| 68 | // shift operators |
| 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() |
| 74 | ); |
| 75 | }; |
| 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() |
| 81 | ); |
| 82 | }; |
| 83 | // bitwise operators |
| 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() |
| 89 | ); |
| 90 | }; |
| 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() |
| 96 | ); |
| 97 | }; |
| 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() |
| 103 | ); |
| 104 | }; |
| 105 | }; |
| 106 | |
| 107 | } // safe_numerics |
| 108 | } // boost |
| 109 | |
| 110 | #endif // BOOST_SAFE_NUMERICS_NATIVE_HPP |
| 111 | |