]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_TYPE_TRAITS_DETAIL_COMMON_ARITHMETIC_TYPE_HPP_INCLUDED |
2 | #define BOOST_TYPE_TRAITS_DETAIL_COMMON_ARITHMETIC_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 | ||
14 | namespace boost | |
15 | { | |
16 | ||
17 | namespace type_traits_detail | |
18 | { | |
19 | ||
20 | template<int I> struct arithmetic_type; | |
21 | ||
22 | // Types bool, char, char16_t, char32_t, wchar_t, | |
23 | // and the signed and unsigned integer types are | |
24 | // collectively called integral types | |
25 | ||
26 | template<> struct arithmetic_type<1> | |
27 | { | |
28 | typedef bool type; | |
29 | typedef char (&result_type) [1]; | |
30 | }; | |
31 | ||
32 | template<> struct arithmetic_type<2> | |
33 | { | |
34 | typedef char type; | |
35 | typedef char (&result_type) [2]; | |
36 | }; | |
37 | ||
38 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
39 | ||
40 | template<> struct arithmetic_type<3> | |
41 | { | |
42 | typedef wchar_t type; | |
43 | typedef char (&result_type) [3]; | |
44 | }; | |
45 | ||
46 | #endif | |
47 | ||
48 | // There are five standard signed integer types: | |
49 | // “signed char”, “short int”, “int”, “long int”, and “long long int”. | |
50 | ||
51 | template<> struct arithmetic_type<4> | |
52 | { | |
53 | typedef signed char type; | |
54 | typedef char (&result_type) [4]; | |
55 | }; | |
56 | ||
57 | template<> struct arithmetic_type<5> | |
58 | { | |
59 | typedef short type; | |
60 | typedef char (&result_type) [5]; | |
61 | }; | |
62 | ||
63 | template<> struct arithmetic_type<6> | |
64 | { | |
65 | typedef int type; | |
66 | typedef char (&result_type) [6]; | |
67 | }; | |
68 | ||
69 | template<> struct arithmetic_type<7> | |
70 | { | |
71 | typedef long type; | |
72 | typedef char (&result_type) [7]; | |
73 | }; | |
74 | ||
75 | template<> struct arithmetic_type<8> | |
76 | { | |
77 | typedef boost::long_long_type type; | |
78 | typedef char (&result_type) [8]; | |
79 | }; | |
80 | ||
81 | // For each of the standard signed integer types, there exists a corresponding | |
82 | // (but different) standard unsigned integer type: “unsigned char”, “unsigned short int”, | |
83 | // “unsigned int”, “unsigned long int”, and “unsigned long long int” | |
84 | ||
85 | template<> struct arithmetic_type<9> | |
86 | { | |
87 | typedef unsigned char type; | |
88 | typedef char (&result_type) [9]; | |
89 | }; | |
90 | ||
91 | template<> struct arithmetic_type<10> | |
92 | { | |
93 | typedef unsigned short type; | |
94 | typedef char (&result_type) [10]; | |
95 | }; | |
96 | ||
97 | template<> struct arithmetic_type<11> | |
98 | { | |
99 | typedef unsigned int type; | |
100 | typedef char (&result_type) [11]; | |
101 | }; | |
102 | ||
103 | template<> struct arithmetic_type<12> | |
104 | { | |
105 | typedef unsigned long type; | |
106 | typedef char (&result_type) [12]; | |
107 | }; | |
108 | ||
109 | template<> struct arithmetic_type<13> | |
110 | { | |
111 | typedef boost::ulong_long_type type; | |
112 | typedef char (&result_type) [13]; | |
113 | }; | |
114 | ||
115 | // There are three floating point types: float, double, and long double. | |
116 | ||
117 | template<> struct arithmetic_type<14> | |
118 | { | |
119 | typedef float type; | |
120 | typedef char (&result_type) [14]; | |
121 | }; | |
122 | ||
123 | template<> struct arithmetic_type<15> | |
124 | { | |
125 | typedef double type; | |
126 | typedef char (&result_type) [15]; | |
127 | }; | |
128 | ||
129 | template<> struct arithmetic_type<16> | |
130 | { | |
131 | typedef long double type; | |
132 | typedef char (&result_type) [16]; | |
133 | }; | |
134 | ||
135 | #if !defined( BOOST_NO_CXX11_CHAR16_T ) | |
136 | ||
137 | template<> struct arithmetic_type<17> | |
138 | { | |
139 | typedef char16_t type; | |
140 | typedef char (&result_type) [17]; | |
141 | }; | |
142 | ||
143 | #endif | |
144 | ||
145 | #if !defined( BOOST_NO_CXX11_CHAR32_T ) | |
146 | ||
147 | template<> struct arithmetic_type<18> | |
148 | { | |
149 | typedef char32_t type; | |
150 | typedef char (&result_type) [18]; | |
151 | }; | |
152 | ||
153 | #endif | |
154 | ||
155 | #if defined( BOOST_HAS_INT128 ) | |
156 | ||
157 | template<> struct arithmetic_type<19> | |
158 | { | |
159 | typedef boost::int128_type type; | |
160 | typedef char (&result_type) [19]; | |
161 | }; | |
162 | ||
163 | template<> struct arithmetic_type<20> | |
164 | { | |
165 | typedef boost::uint128_type type; | |
166 | typedef char (&result_type) [20]; | |
167 | }; | |
168 | ||
169 | #endif | |
170 | ||
171 | template<class T, class U> class common_arithmetic_type | |
172 | { | |
173 | private: | |
174 | ||
175 | static arithmetic_type<1>::result_type select( arithmetic_type<1>::type ); | |
176 | static arithmetic_type<2>::result_type select( arithmetic_type<2>::type ); | |
177 | #ifndef BOOST_NO_INTRINSIC_WCHAR_T | |
178 | static arithmetic_type<3>::result_type select( arithmetic_type<3>::type ); | |
179 | #endif | |
180 | static arithmetic_type<4>::result_type select( arithmetic_type<4>::type ); | |
181 | static arithmetic_type<5>::result_type select( arithmetic_type<5>::type ); | |
182 | static arithmetic_type<6>::result_type select( arithmetic_type<6>::type ); | |
183 | static arithmetic_type<7>::result_type select( arithmetic_type<7>::type ); | |
184 | static arithmetic_type<8>::result_type select( arithmetic_type<8>::type ); | |
185 | static arithmetic_type<9>::result_type select( arithmetic_type<9>::type ); | |
186 | static arithmetic_type<10>::result_type select( arithmetic_type<10>::type ); | |
187 | static arithmetic_type<11>::result_type select( arithmetic_type<11>::type ); | |
188 | static arithmetic_type<12>::result_type select( arithmetic_type<12>::type ); | |
189 | static arithmetic_type<13>::result_type select( arithmetic_type<13>::type ); | |
190 | static arithmetic_type<14>::result_type select( arithmetic_type<14>::type ); | |
191 | static arithmetic_type<15>::result_type select( arithmetic_type<15>::type ); | |
192 | static arithmetic_type<16>::result_type select( arithmetic_type<16>::type ); | |
193 | ||
194 | #if !defined( BOOST_NO_CXX11_CHAR16_T ) | |
195 | static arithmetic_type<17>::result_type select( arithmetic_type<17>::type ); | |
196 | #endif | |
197 | ||
198 | #if !defined( BOOST_NO_CXX11_CHAR32_T ) | |
199 | static arithmetic_type<18>::result_type select( arithmetic_type<18>::type ); | |
200 | #endif | |
201 | ||
202 | #if defined( BOOST_HAS_INT128 ) | |
203 | static arithmetic_type<19>::result_type select( arithmetic_type<19>::type ); | |
204 | static arithmetic_type<20>::result_type select( arithmetic_type<20>::type ); | |
205 | #endif | |
206 | ||
207 | static bool cond(); | |
208 | ||
209 | public: | |
210 | ||
211 | typedef typename arithmetic_type< sizeof(select( cond()? T(): U() )) >::type type; | |
212 | }; | |
213 | ||
214 | } // namespace type_traits_detail | |
215 | ||
216 | } // namespace boost | |
217 | ||
218 | #endif // #ifndef BOOST_TYPE_TRAITS_DETAIL_COMMON_ARITHMETIC_TYPE_HPP_INCLUDED |