1 // The -*- C++ -*- type traits classes for internal use in libstdc++
3 // Copyright (C) 2000-2018 Free Software Foundation, Inc.
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)
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
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
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
69 namespace std
_GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 struct __true_type
{ };
74 struct __false_type
{ };
78 { typedef __false_type __type
; };
81 struct __truth_type
<true>
82 { typedef __true_type __type
; };
84 // N.B. The conversions to bool are needed due to the issue
85 // explained in c++/19404.
86 template<class _Sp
, class _Tp
>
89 enum { __value
= bool(_Sp::__value
) || bool(_Tp::__value
) };
90 typedef typename __truth_type
<__value
>::__type __type
;
93 // Compare for equality of types.
94 template<typename
, typename
>
98 typedef __false_type __type
;
101 template<typename _Tp
>
102 struct __are_same
<_Tp
, _Tp
>
104 enum { __value
= 1 };
105 typedef __true_type __type
;
108 // Holds if the template-argument is a void type.
109 template<typename _Tp
>
112 enum { __value
= 0 };
113 typedef __false_type __type
;
117 struct __is_void
<void>
119 enum { __value
= 1 };
120 typedef __true_type __type
;
126 template<typename _Tp
>
129 enum { __value
= 0 };
130 typedef __false_type __type
;
133 // Thirteen specializations (yes there are eleven standard integer
134 // types; <em>long long</em> and <em>unsigned long long</em> are
135 // supported as extensions). Up to four target-specific __int<N>
136 // types are supported as well.
138 struct __is_integer
<bool>
140 enum { __value
= 1 };
141 typedef __true_type __type
;
145 struct __is_integer
<char>
147 enum { __value
= 1 };
148 typedef __true_type __type
;
152 struct __is_integer
<signed char>
154 enum { __value
= 1 };
155 typedef __true_type __type
;
159 struct __is_integer
<unsigned char>
161 enum { __value
= 1 };
162 typedef __true_type __type
;
165 # ifdef _GLIBCXX_USE_WCHAR_T
167 struct __is_integer
<wchar_t>
169 enum { __value
= 1 };
170 typedef __true_type __type
;
174 #if __cplusplus >= 201103L
176 struct __is_integer
<char16_t
>
178 enum { __value
= 1 };
179 typedef __true_type __type
;
183 struct __is_integer
<char32_t
>
185 enum { __value
= 1 };
186 typedef __true_type __type
;
191 struct __is_integer
<short>
193 enum { __value
= 1 };
194 typedef __true_type __type
;
198 struct __is_integer
<unsigned short>
200 enum { __value
= 1 };
201 typedef __true_type __type
;
205 struct __is_integer
<int>
207 enum { __value
= 1 };
208 typedef __true_type __type
;
212 struct __is_integer
<unsigned int>
214 enum { __value
= 1 };
215 typedef __true_type __type
;
219 struct __is_integer
<long>
221 enum { __value
= 1 };
222 typedef __true_type __type
;
226 struct __is_integer
<unsigned long>
228 enum { __value
= 1 };
229 typedef __true_type __type
;
233 struct __is_integer
<long long>
235 enum { __value
= 1 };
236 typedef __true_type __type
;
240 struct __is_integer
<unsigned long long>
242 enum { __value
= 1 };
243 typedef __true_type __type
;
246 #define __INT_N(TYPE) \
248 struct __is_integer<TYPE> \
250 enum { __value = 1 }; \
251 typedef __true_type __type; \
254 struct __is_integer<unsigned TYPE> \
256 enum { __value = 1 }; \
257 typedef __true_type __type; \
260 #ifdef __GLIBCXX_TYPE_INT_N_0
261 __INT_N(__GLIBCXX_TYPE_INT_N_0
)
263 #ifdef __GLIBCXX_TYPE_INT_N_1
264 __INT_N(__GLIBCXX_TYPE_INT_N_1
)
266 #ifdef __GLIBCXX_TYPE_INT_N_2
267 __INT_N(__GLIBCXX_TYPE_INT_N_2
)
269 #ifdef __GLIBCXX_TYPE_INT_N_3
270 __INT_N(__GLIBCXX_TYPE_INT_N_3
)
276 // Floating point types
278 template<typename _Tp
>
281 enum { __value
= 0 };
282 typedef __false_type __type
;
285 // three specializations (float, double and 'long double')
287 struct __is_floating
<float>
289 enum { __value
= 1 };
290 typedef __true_type __type
;
294 struct __is_floating
<double>
296 enum { __value
= 1 };
297 typedef __true_type __type
;
301 struct __is_floating
<long double>
303 enum { __value
= 1 };
304 typedef __true_type __type
;
310 template<typename _Tp
>
313 enum { __value
= 0 };
314 typedef __false_type __type
;
317 template<typename _Tp
>
318 struct __is_pointer
<_Tp
*>
320 enum { __value
= 1 };
321 typedef __true_type __type
;
325 // An arithmetic type is an integer type or a floating point type
327 template<typename _Tp
>
328 struct __is_arithmetic
329 : public __traitor
<__is_integer
<_Tp
>, __is_floating
<_Tp
> >
333 // A scalar type is an arithmetic type or a pointer type
335 template<typename _Tp
>
337 : public __traitor
<__is_arithmetic
<_Tp
>, __is_pointer
<_Tp
> >
341 // For use in std::copy and std::find overloads for streambuf iterators.
343 template<typename _Tp
>
346 enum { __value
= 0 };
347 typedef __false_type __type
;
351 struct __is_char
<char>
353 enum { __value
= 1 };
354 typedef __true_type __type
;
357 #ifdef _GLIBCXX_USE_WCHAR_T
359 struct __is_char
<wchar_t>
361 enum { __value
= 1 };
362 typedef __true_type __type
;
366 template<typename _Tp
>
369 enum { __value
= 0 };
370 typedef __false_type __type
;
374 struct __is_byte
<char>
376 enum { __value
= 1 };
377 typedef __true_type __type
;
381 struct __is_byte
<signed char>
383 enum { __value
= 1 };
384 typedef __true_type __type
;
388 struct __is_byte
<unsigned char>
390 enum { __value
= 1 };
391 typedef __true_type __type
;
394 #if __cplusplus >= 201703L
395 enum class byte
: unsigned char;
398 struct __is_byte
<byte
>
400 enum { __value
= 1 };
401 typedef __true_type __type
;
406 // Move iterator type
408 template<typename _Tp
>
409 struct __is_move_iterator
411 enum { __value
= 0 };
412 typedef __false_type __type
;
415 // Fallback implementation of the function in bits/stl_iterator.h used to
416 // remove the move_iterator wrapper.
417 template<typename _Iterator
>
419 __miter_base(_Iterator __it
)
422 _GLIBCXX_END_NAMESPACE_VERSION
426 #endif //_CPP_TYPE_TRAITS_H