1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
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)
84 // When using clang, suppress warnings about possible keywords (such as
85 // __is_void, __is_pod, etc) being used as identifiers.
86 #pragma clang diagnostic push
87 #pragma clang diagnostic ignored "-Wkeyword-compat"
92 // NB: g++ can not compile these if declared within the class
95 typedef char __two[2];
97 template<typename _Tp>
98 __one __test_type(int _Tp::*);
99 template<typename _Tp>
100 __two& __test_type(...);
101 } // namespace __detail
104 struct __true_type { };
105 struct __false_type { };
109 { typedef __false_type __type; };
112 struct __truth_type<true>
113 { typedef __true_type __type; };
115 // N.B. The conversions to bool are needed due to the issue
116 // explained in c++/19404.
117 template<class _Sp, class _Tp>
120 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
121 typedef typename __truth_type<__value>::__type __type;
124 // Compare for equality of types.
125 template<typename, typename>
128 enum { __value = 0 };
129 typedef __false_type __type;
132 template<typename _Tp>
133 struct __are_same<_Tp, _Tp>
135 enum { __value = 1 };
136 typedef __true_type __type;
139 // Holds if the template-argument is a void type.
140 template<typename _Tp>
143 enum { __value = 0 };
144 typedef __false_type __type;
148 struct __is_void<void>
150 enum { __value = 1 };
151 typedef __true_type __type;
157 template<typename _Tp>
160 enum { __value = 0 };
161 typedef __false_type __type;
164 // Thirteen specializations (yes there are eleven standard integer
165 // types; 'long long' and 'unsigned long long' are supported as
168 struct __is_integer<bool>
170 enum { __value = 1 };
171 typedef __true_type __type;
175 struct __is_integer<char>
177 enum { __value = 1 };
178 typedef __true_type __type;
182 struct __is_integer<signed char>
184 enum { __value = 1 };
185 typedef __true_type __type;
189 struct __is_integer<unsigned char>
191 enum { __value = 1 };
192 typedef __true_type __type;
195 # ifdef _GLIBCXX_USE_WCHAR_T
197 struct __is_integer<wchar_t>
199 enum { __value = 1 };
200 typedef __true_type __type;
205 struct __is_integer<short>
207 enum { __value = 1 };
208 typedef __true_type __type;
212 struct __is_integer<unsigned short>
214 enum { __value = 1 };
215 typedef __true_type __type;
219 struct __is_integer<int>
221 enum { __value = 1 };
222 typedef __true_type __type;
226 struct __is_integer<unsigned int>
228 enum { __value = 1 };
229 typedef __true_type __type;
233 struct __is_integer<long>
235 enum { __value = 1 };
236 typedef __true_type __type;
240 struct __is_integer<unsigned long>
242 enum { __value = 1 };
243 typedef __true_type __type;
247 struct __is_integer<long long>
249 enum { __value = 1 };
250 typedef __true_type __type;
254 struct __is_integer<unsigned long long>
256 enum { __value = 1 };
257 typedef __true_type __type;
261 // Floating point types
263 template<typename _Tp>
266 enum { __value = 0 };
267 typedef __false_type __type;
270 // three specializations (float, double and 'long double')
272 struct __is_floating<float>
274 enum { __value = 1 };
275 typedef __true_type __type;
279 struct __is_floating<double>
281 enum { __value = 1 };
282 typedef __true_type __type;
286 struct __is_floating<long double>
288 enum { __value = 1 };
289 typedef __true_type __type;
295 template<typename _Tp>
298 enum { __value = 0 };
299 typedef __false_type __type;
302 template<typename _Tp>
303 struct __is_pointer<_Tp*>
305 enum { __value = 1 };
306 typedef __true_type __type;
310 // Normal iterator type
312 template<typename _Tp>
313 struct __is_normal_iterator
315 enum { __value = 0 };
316 typedef __false_type __type;
319 template<typename _Iterator, typename _Container>
320 struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
323 enum { __value = 1 };
324 typedef __true_type __type;
328 // An arithmetic type is an integer type or a floating point type
330 template<typename _Tp>
331 struct __is_arithmetic
332 : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
336 // A fundamental type is `void' or and arithmetic type
338 template<typename _Tp>
339 struct __is_fundamental
340 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
344 // A scalar type is an arithmetic type or a pointer type
346 template<typename _Tp>
348 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
351 // For the immediate use, the following is a good approximation.
352 template<typename _Tp>
357 __value = (sizeof(__detail::__test_type<_Tp>(0))
358 != sizeof(__detail::__one))
363 // A stripped-down version of std::tr1::is_empty
365 template<typename _Tp>
371 template<typename _Up>
378 __value = sizeof(__first<_Tp>) == sizeof(__second<_Tp>)
383 // For use in std::copy and std::find overloads for streambuf iterators.
385 template<typename _Tp>
388 enum { __value = 0 };
389 typedef __false_type __type;
393 struct __is_char<char>
395 enum { __value = 1 };
396 typedef __true_type __type;
399 #ifdef _GLIBCXX_USE_WCHAR_T
401 struct __is_char<wchar_t>
403 enum { __value = 1 };
404 typedef __true_type __type;
409 #pragma clang diagnostic pop
412 _GLIBCXX_END_NAMESPACE
414 #endif //_CPP_TYPE_TRAITS_H