2015-07-16 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / cpp_type_traits.h
blobb66b61f2c65dff57a78949f481a92c3d258f7459
1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/cpp_type_traits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ext/type_traits}
30 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
32 #ifndef _CPP_TYPE_TRAITS_H
33 #define _CPP_TYPE_TRAITS_H 1
35 #pragma GCC system_header
37 #include <bits/c++config.h>
40 // This file provides some compile-time information about various types.
41 // These representations were designed, on purpose, to be constant-expressions
42 // and not types as found in <bits/type_traits.h>. In particular, they
43 // can be used in control structures and the optimizer hopefully will do
44 // the obvious thing.
46 // Why integral expressions, and not functions nor types?
47 // Firstly, these compile-time entities are used as template-arguments
48 // so function return values won't work: We need compile-time entities.
49 // We're left with types and constant integral expressions.
50 // Secondly, from the point of view of ease of use, type-based compile-time
51 // information is -not- *that* convenient. On has to write lots of
52 // overloaded functions and to hope that the compiler will select the right
53 // one. As a net effect, the overall structure isn't very clear at first
54 // glance.
55 // Thirdly, partial ordering and overload resolution (of function templates)
56 // is highly costly in terms of compiler-resource. It is a Good Thing to
57 // keep these resource consumption as least as possible.
59 // See valarray_array.h for a case use.
61 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
63 // Update 2005: types are also provided and <bits/type_traits.h> has been
64 // removed.
67 namespace std _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 struct __true_type { };
72 struct __false_type { };
74 template<bool>
75 struct __truth_type
76 { typedef __false_type __type; };
78 template<>
79 struct __truth_type<true>
80 { typedef __true_type __type; };
82 // N.B. The conversions to bool are needed due to the issue
83 // explained in c++/19404.
84 template<class _Sp, class _Tp>
85 struct __traitor
87 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
88 typedef typename __truth_type<__value>::__type __type;
91 // Compare for equality of types.
92 template<typename, typename>
93 struct __are_same
95 enum { __value = 0 };
96 typedef __false_type __type;
99 template<typename _Tp>
100 struct __are_same<_Tp, _Tp>
102 enum { __value = 1 };
103 typedef __true_type __type;
106 // Holds if the template-argument is a void type.
107 template<typename _Tp>
108 struct __is_void
110 enum { __value = 0 };
111 typedef __false_type __type;
114 template<>
115 struct __is_void<void>
117 enum { __value = 1 };
118 typedef __true_type __type;
122 // Integer types
124 template<typename _Tp>
125 struct __is_integer
127 enum { __value = 0 };
128 typedef __false_type __type;
131 // Thirteen specializations (yes there are eleven standard integer
132 // types; <em>long long</em> and <em>unsigned long long</em> are
133 // supported as extensions). Up to four target-specific __int<N>
134 // types are supported as well.
135 template<>
136 struct __is_integer<bool>
138 enum { __value = 1 };
139 typedef __true_type __type;
142 template<>
143 struct __is_integer<char>
145 enum { __value = 1 };
146 typedef __true_type __type;
149 template<>
150 struct __is_integer<signed char>
152 enum { __value = 1 };
153 typedef __true_type __type;
156 template<>
157 struct __is_integer<unsigned char>
159 enum { __value = 1 };
160 typedef __true_type __type;
163 # ifdef _GLIBCXX_USE_WCHAR_T
164 template<>
165 struct __is_integer<wchar_t>
167 enum { __value = 1 };
168 typedef __true_type __type;
170 # endif
172 #if __cplusplus >= 201103L
173 template<>
174 struct __is_integer<char16_t>
176 enum { __value = 1 };
177 typedef __true_type __type;
180 template<>
181 struct __is_integer<char32_t>
183 enum { __value = 1 };
184 typedef __true_type __type;
186 #endif
188 template<>
189 struct __is_integer<short>
191 enum { __value = 1 };
192 typedef __true_type __type;
195 template<>
196 struct __is_integer<unsigned short>
198 enum { __value = 1 };
199 typedef __true_type __type;
202 template<>
203 struct __is_integer<int>
205 enum { __value = 1 };
206 typedef __true_type __type;
209 template<>
210 struct __is_integer<unsigned int>
212 enum { __value = 1 };
213 typedef __true_type __type;
216 template<>
217 struct __is_integer<long>
219 enum { __value = 1 };
220 typedef __true_type __type;
223 template<>
224 struct __is_integer<unsigned long>
226 enum { __value = 1 };
227 typedef __true_type __type;
230 template<>
231 struct __is_integer<long long>
233 enum { __value = 1 };
234 typedef __true_type __type;
237 template<>
238 struct __is_integer<unsigned long long>
240 enum { __value = 1 };
241 typedef __true_type __type;
244 #define __INT_N(TYPE) \
245 template<> \
246 struct __is_integer<TYPE> \
248 enum { __value = 1 }; \
249 typedef __true_type __type; \
250 }; \
251 template<> \
252 struct __is_integer<unsigned TYPE> \
254 enum { __value = 1 }; \
255 typedef __true_type __type; \
258 #ifdef __GLIBCXX_TYPE_INT_N_0
259 __INT_N(__GLIBCXX_TYPE_INT_N_0)
260 #endif
261 #ifdef __GLIBCXX_TYPE_INT_N_1
262 __INT_N(__GLIBCXX_TYPE_INT_N_1)
263 #endif
264 #ifdef __GLIBCXX_TYPE_INT_N_2
265 __INT_N(__GLIBCXX_TYPE_INT_N_2)
266 #endif
267 #ifdef __GLIBCXX_TYPE_INT_N_3
268 __INT_N(__GLIBCXX_TYPE_INT_N_3)
269 #endif
271 #undef __INT_N
274 // Floating point types
276 template<typename _Tp>
277 struct __is_floating
279 enum { __value = 0 };
280 typedef __false_type __type;
283 // three specializations (float, double and 'long double')
284 template<>
285 struct __is_floating<float>
287 enum { __value = 1 };
288 typedef __true_type __type;
291 template<>
292 struct __is_floating<double>
294 enum { __value = 1 };
295 typedef __true_type __type;
298 template<>
299 struct __is_floating<long double>
301 enum { __value = 1 };
302 typedef __true_type __type;
306 // Pointer types
308 template<typename _Tp>
309 struct __is_pointer
311 enum { __value = 0 };
312 typedef __false_type __type;
315 template<typename _Tp>
316 struct __is_pointer<_Tp*>
318 enum { __value = 1 };
319 typedef __true_type __type;
323 // An arithmetic type is an integer type or a floating point type
325 template<typename _Tp>
326 struct __is_arithmetic
327 : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
328 { };
331 // A scalar type is an arithmetic type or a pointer type
333 template<typename _Tp>
334 struct __is_scalar
335 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
336 { };
339 // For use in std::copy and std::find overloads for streambuf iterators.
341 template<typename _Tp>
342 struct __is_char
344 enum { __value = 0 };
345 typedef __false_type __type;
348 template<>
349 struct __is_char<char>
351 enum { __value = 1 };
352 typedef __true_type __type;
355 #ifdef _GLIBCXX_USE_WCHAR_T
356 template<>
357 struct __is_char<wchar_t>
359 enum { __value = 1 };
360 typedef __true_type __type;
362 #endif
364 template<typename _Tp>
365 struct __is_byte
367 enum { __value = 0 };
368 typedef __false_type __type;
371 template<>
372 struct __is_byte<char>
374 enum { __value = 1 };
375 typedef __true_type __type;
378 template<>
379 struct __is_byte<signed char>
381 enum { __value = 1 };
382 typedef __true_type __type;
385 template<>
386 struct __is_byte<unsigned char>
388 enum { __value = 1 };
389 typedef __true_type __type;
393 // Move iterator type
395 template<typename _Tp>
396 struct __is_move_iterator
398 enum { __value = 0 };
399 typedef __false_type __type;
402 // Fallback implementation of the function in bits/stl_iterator.h used to
403 // remove the move_iterator wrapper.
404 template<typename _Iterator>
405 inline _Iterator
406 __miter_base(_Iterator __it)
407 { return __it; }
409 _GLIBCXX_END_NAMESPACE_VERSION
410 } // namespace
412 #endif //_CPP_TYPE_TRAITS_H