Auxiliary Arithmetic Algorithms
traits.hpp
1 #pragma once
2 
3 #include <type_traits>
4 
5 namespace aaa {
6 
7 template<typename Container>
8 using value_type = typename Container::value_type;
9 
10 template<typename Iterator>
11 using value_type_i = typename std::iterator_traits<Iterator>::value_type;
12 
13 template<typename Container>
14 using iterator = typename Container::iterator;
15 
16 template<typename Container>
17 using const_iterator = typename Container::const_iterator;
18 
19 
20 template<typename T> struct sqrt_type { using type = double; };
21 template<> struct sqrt_type<float> { using type = float; };
22 template<> struct sqrt_type<long double> { using type = long double; };
23 template<typename T> using sqrt_type_t = typename sqrt_type<T>::type;
24 
25 template<typename Container>
26 using sqrt_value_type = sqrt_type_t<value_type<Container>>;
27 
28 template<typename Iterator>
29 using sqrt_value_type_i = sqrt_type_t<value_type_i<Iterator>>;
30 
31 
32 // This is what we want. However, it requires Expression SFINAE, which is not
33 // yet supported in MSVC, unless MSVC uses Clang.
34 #if !_MSC_VER || __clang__
35 
36 template<typename A, typename B, typename C>
37 using check_sum = decltype(C{ A{} + B{} })*;
38 
39 template<typename A, typename B, typename C>
40 using check_difference = decltype(C{ A{} - B{} })*;
41 
42 template<typename A, typename B, typename C>
43 using check_product = decltype(C{ A{} * B{} })*;
44 
45 template<typename A, typename B, typename C>
46 using check_ratio = decltype(C{ A{} / B{} })*;
47 
48 #else // Fallback for MSVC without Clang.
49 
50 template<typename A, typename B, typename C = B>
51 using enable_if_same = typename std::enable_if<
52  std::is_same<A, B>::value && std::is_same<B, C>::value, void*>::type;
53 
54 template<typename A, typename B, typename C>
55 using check_sum = enable_if_same<A, B, C>;
56 
57 template<typename A, typename B, typename C>
58 using check_difference = enable_if_same<A, B, C>;
59 
60 template<typename A, typename B, typename C>
61 using check_product = enable_if_same<A, B, C>;
62 
63 template<typename A, typename B, typename C>
64 using check_ratio = enable_if_same<A, B, C>;
65 
66 #endif
67 
68 } // namespace aaa
Definition: add.hpp:7
Definition: traits.hpp:20