1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file bits/cpp_type_traits.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{ext/type_traits}
31 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
33 #ifndef _CPP_TYPE_TRAITS_H
34 #define _CPP_TYPE_TRAITS_H 1
36 #pragma GCC system_header
38 #include <bits/c++config.h>
41 // This file provides some compile-time information about various types.
42 // These representations were designed, on purpose, to be constant-expressions
43 // and not types as found in <bits/type_traits.h>. In particular, they
44 // can be used in control structures and the optimizer hopefully will do
47 // Why integral expressions, and not functions nor types?
48 // Firstly, these compile-time entities are used as template-arguments
49 // so function return values won't work: We need compile-time entities.
50 // We're left with types and constant integral expressions.
51 // Secondly, from the point of view of ease of use, type-based compile-time
52 // information is -not- *that* convenient. On has to write lots of
53 // overloaded functions and to hope that the compiler will select the right
54 // one. As a net effect, the overall structure isn't very clear at first
56 // Thirdly, partial ordering and overload resolution (of function templates)
57 // is highly costly in terms of compiler-resource. It is a Good Thing to
58 // keep these resource consumption as least as possible.
60 // See valarray_array.h for a case use.
62 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
64 // Update 2005: types are also provided and <bits/type_traits.h> has been
68 // Forward declaration hack, should really include this from somewhere.
69 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 template<typename _Iterator, typename _Container>
74 class __normal_iterator;
76 _GLIBCXX_END_NAMESPACE_VERSION
79 namespace std _GLIBCXX_VISIBILITY(default)
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
83 struct __true_type { };
84 struct __false_type { };
88 { typedef __false_type __type; };
91 struct __truth_type<true>
92 { typedef __true_type __type; };
94 // N.B. The conversions to bool are needed due to the issue
95 // explained in c++/19404.
96 template<class _Sp, class _Tp>
99 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
100 typedef typename __truth_type<__value>::__type __type;
103 // Compare for equality of types.
104 template<typename, typename>
107 enum { __value = 0 };
108 typedef __false_type __type;
111 template<typename _Tp>
112 struct __are_same<_Tp, _Tp>
114 enum { __value = 1 };
115 typedef __true_type __type;
118 // Holds if the template-argument is a void type.
119 template<typename _Tp>
122 enum { __value = 0 };
123 typedef __false_type __type;
127 struct __is_void<void>
129 enum { __value = 1 };
130 typedef __true_type __type;
136 template<typename _Tp>
139 enum { __value = 0 };
140 typedef __false_type __type;
143 // Thirteen specializations (yes there are eleven standard integer
144 // types; <em>long long</em> and <em>unsigned long long</em> are
145 // supported as extensions)
147 struct __is_integer<bool>
149 enum { __value = 1 };
150 typedef __true_type __type;
154 struct __is_integer<char>
156 enum { __value = 1 };
157 typedef __true_type __type;
161 struct __is_integer<signed char>
163 enum { __value = 1 };
164 typedef __true_type __type;
168 struct __is_integer<unsigned char>
170 enum { __value = 1 };
171 typedef __true_type __type;
174 # ifdef _GLIBCXX_USE_WCHAR_T
176 struct __is_integer<wchar_t>
178 enum { __value = 1 };
179 typedef __true_type __type;
183 #ifdef __GXX_EXPERIMENTAL_CXX0X__
185 struct __is_integer<char16_t>
187 enum { __value = 1 };
188 typedef __true_type __type;
192 struct __is_integer<char32_t>
194 enum { __value = 1 };
195 typedef __true_type __type;
200 struct __is_integer<short>
202 enum { __value = 1 };
203 typedef __true_type __type;
207 struct __is_integer<unsigned short>
209 enum { __value = 1 };
210 typedef __true_type __type;
214 struct __is_integer<int>
216 enum { __value = 1 };
217 typedef __true_type __type;
221 struct __is_integer<unsigned int>
223 enum { __value = 1 };
224 typedef __true_type __type;
228 struct __is_integer<long>
230 enum { __value = 1 };
231 typedef __true_type __type;
235 struct __is_integer<unsigned long>
237 enum { __value = 1 };
238 typedef __true_type __type;
242 struct __is_integer<long long>
244 enum { __value = 1 };
245 typedef __true_type __type;
249 struct __is_integer<unsigned long long>
251 enum { __value = 1 };
252 typedef __true_type __type;
256 // Floating point types
258 template<typename _Tp>
261 enum { __value = 0 };
262 typedef __false_type __type;
265 // three specializations (float, double and 'long double')
267 struct __is_floating<float>
269 enum { __value = 1 };
270 typedef __true_type __type;
274 struct __is_floating<double>
276 enum { __value = 1 };
277 typedef __true_type __type;
281 struct __is_floating<long double>
283 enum { __value = 1 };
284 typedef __true_type __type;
290 template<typename _Tp>
293 enum { __value = 0 };
294 typedef __false_type __type;
297 template<typename _Tp>
298 struct __is_pointer<_Tp*>
300 enum { __value = 1 };
301 typedef __true_type __type;
305 // Normal iterator type
307 template<typename _Tp>
308 struct __is_normal_iterator
310 enum { __value = 0 };
311 typedef __false_type __type;
314 template<typename _Iterator, typename _Container>
315 struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
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> >
331 // A fundamental type is `void' or and arithmetic type
333 template<typename _Tp>
334 struct __is_fundamental
335 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
339 // A scalar type is an arithmetic type or a pointer type
341 template<typename _Tp>
343 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
347 // For use in std::copy and std::find overloads for streambuf iterators.
349 template<typename _Tp>
352 enum { __value = 0 };
353 typedef __false_type __type;
357 struct __is_char<char>
359 enum { __value = 1 };
360 typedef __true_type __type;
363 #ifdef _GLIBCXX_USE_WCHAR_T
365 struct __is_char<wchar_t>
367 enum { __value = 1 };
368 typedef __true_type __type;
372 template<typename _Tp>
375 enum { __value = 0 };
376 typedef __false_type __type;
380 struct __is_byte<char>
382 enum { __value = 1 };
383 typedef __true_type __type;
387 struct __is_byte<signed char>
389 enum { __value = 1 };
390 typedef __true_type __type;
394 struct __is_byte<unsigned char>
396 enum { __value = 1 };
397 typedef __true_type __type;
401 // Move iterator type
403 template<typename _Tp>
404 struct __is_move_iterator
406 enum { __value = 0 };
407 typedef __false_type __type;
410 #ifdef __GXX_EXPERIMENTAL_CXX0X__
411 template<typename _Iterator>
414 template<typename _Iterator>
415 struct __is_move_iterator< move_iterator<_Iterator> >
417 enum { __value = 1 };
418 typedef __true_type __type;
422 _GLIBCXX_END_NAMESPACE_VERSION
425 #endif //_CPP_TYPE_TRAITS_H