1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file cpp_type_traits.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 // Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
38 #ifndef _CPP_TYPE_TRAITS_H
39 #define _CPP_TYPE_TRAITS_H 1
41 #pragma GCC system_header
43 #include <bits/c++config.h>
46 // This file provides some compile-time information about various types.
47 // These representations were designed, on purpose, to be constant-expressions
48 // and not types as found in <bits/type_traits.h>. In particular, they
49 // can be used in control structures and the optimizer hopefully will do
52 // Why integral expressions, and not functions nor types?
53 // Firstly, these compile-time entities are used as template-arguments
54 // so function return values won't work: We need compile-time entities.
55 // We're left with types and constant integral expressions.
56 // Secondly, from the point of view of ease of use, type-based compile-time
57 // information is -not- *that* convenient. On has to write lots of
58 // overloaded functions and to hope that the compiler will select the right
59 // one. As a net effect, the overall structure isn't very clear at first
61 // Thirdly, partial ordering and overload resolution (of function templates)
62 // is highly costly in terms of compiler-resource. It is a Good Thing to
63 // keep these resource consumption as least as possible.
65 // See valarray_array.h for a case use.
67 // -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
69 // Update 2005: types are also provided and <bits/type_traits.h> has been
73 // Forward declaration hack, should really include this from somewhere.
74 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
76 template<typename _Iterator
, typename _Container
>
77 class __normal_iterator
;
79 _GLIBCXX_END_NAMESPACE
81 _GLIBCXX_BEGIN_NAMESPACE(std
)
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; 'long long' and 'unsigned long long' are supported as
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
424 #endif //_CPP_TYPE_TRAITS_H