1 | #ifndef BOOST_TYPE_TRAITS_COMMON_TYPE_HPP_INCLUDED |
2 | #define BOOST_TYPE_TRAITS_COMMON_TYPE_HPP_INCLUDED |
3 | |
4 | // |
5 | // Copyright 2015 Peter Dimov |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. |
8 | // See accompanying file LICENSE_1_0.txt or copy at |
9 | // http://www.boost.org/LICENSE_1_0.txt |
10 | // |
11 | |
12 | #include <boost/config.hpp> |
13 | #include <boost/type_traits/decay.hpp> |
14 | #include <boost/type_traits/declval.hpp> |
15 | |
16 | #if defined(BOOST_NO_CXX11_DECLTYPE) |
17 | #include <boost/type_traits/detail/common_type_impl.hpp> |
18 | #endif |
19 | |
20 | #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
21 | #include <boost/type_traits/detail/mp_defer.hpp> |
22 | #endif |
23 | |
24 | namespace boost |
25 | { |
26 | |
27 | // variadic common_type |
28 | |
29 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
30 | |
31 | template<class... T> struct common_type |
32 | { |
33 | }; |
34 | |
35 | #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) |
36 | |
37 | template<class... T> using common_type_t = typename common_type<T...>::type; |
38 | |
39 | namespace type_traits_detail |
40 | { |
41 | |
42 | template<class T1, class T2, class... T> using common_type_fold = common_type_t<common_type_t<T1, T2>, T...>; |
43 | |
44 | } // namespace type_traits_detail |
45 | |
46 | template<class T1, class T2, class... T> |
47 | struct common_type<T1, T2, T...>: type_traits_detail::mp_defer<type_traits_detail::common_type_fold, T1, T2, T...> |
48 | { |
49 | }; |
50 | |
51 | #else |
52 | |
53 | template<class T1, class T2, class... T> |
54 | struct common_type<T1, T2, T...>: common_type<typename common_type<T1, T2>::type, T...> |
55 | { |
56 | }; |
57 | |
58 | #endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) |
59 | |
60 | #else |
61 | |
62 | template< |
63 | class T1 = void, class T2 = void, class T3 = void, |
64 | class T4 = void, class T5 = void, class T6 = void, |
65 | class T7 = void, class T8 = void, class T9 = void |
66 | > |
67 | struct common_type: common_type<typename common_type<T1, T2>::type, T3, T4, T5, T6, T7, T8, T9> |
68 | { |
69 | }; |
70 | |
71 | #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
72 | |
73 | // one argument |
74 | |
75 | template<class T> struct common_type<T>: boost::decay<T> |
76 | { |
77 | }; |
78 | |
79 | // two arguments |
80 | |
81 | namespace type_traits_detail |
82 | { |
83 | |
84 | // binary common_type |
85 | |
86 | #if !defined(BOOST_NO_CXX11_DECLTYPE) |
87 | |
88 | #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
89 | |
90 | #if !defined(BOOST_MSVC) || BOOST_MSVC > 1800 |
91 | |
92 | // internal compiler error on msvc-12.0 |
93 | |
94 | template<class T1, class T2> using builtin_common_type = typename boost::decay<decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() )>::type; |
95 | |
96 | template<class T1, class T2> struct common_type_impl: mp_defer<builtin_common_type, T1, T2> |
97 | { |
98 | }; |
99 | |
100 | #else |
101 | |
102 | template<class T1, class T2> using builtin_common_type = decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() ); |
103 | |
104 | template<class T1, class T2> struct common_type_impl_2: mp_defer<builtin_common_type, T1, T2> |
105 | { |
106 | }; |
107 | |
108 | template<class T1, class T2> using decay_common_type = typename boost::decay<typename common_type_impl_2<T1, T2>::type>::type; |
109 | |
110 | template<class T1, class T2> struct common_type_impl: mp_defer<decay_common_type, T1, T2> |
111 | { |
112 | }; |
113 | |
114 | #endif // !defined(BOOST_MSVC) || BOOST_MSVC > 1800 |
115 | |
116 | #else |
117 | |
118 | template<class T1, class T2> struct common_type_impl: boost::decay<decltype( boost::declval<bool>()? boost::declval<T1>(): boost::declval<T2>() )> |
119 | { |
120 | }; |
121 | |
122 | #endif // #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
123 | |
124 | #endif // #if !defined(BOOST_NO_CXX11_DECLTYPE) |
125 | |
126 | // decay helper |
127 | |
128 | template<class T1, class T2, class T1d = typename boost::decay<T1>::type, class T2d = typename boost::decay<T2>::type> struct common_type_decay_helper: boost::common_type<T1d, T2d> |
129 | { |
130 | }; |
131 | |
132 | template<class T1, class T2> struct common_type_decay_helper<T1, T2, T1, T2>: common_type_impl<T1, T2> |
133 | { |
134 | }; |
135 | |
136 | } // type_traits_detail |
137 | |
138 | template<class T1, class T2> struct common_type<T1, T2>: type_traits_detail::common_type_decay_helper<T1, T2> |
139 | { |
140 | }; |
141 | |
142 | } // namespace boost |
143 | |
144 | #endif // #ifndef BOOST_TYPE_TRAITS_COMMON_TYPE_HPP_INCLUDED |
145 | |