]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/cpp_type_traits.h
*: Use headername alias to associate private includes to public includes.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / cpp_type_traits.h
CommitLineData
725dc051
BK
1// The -*- C++ -*- type traits classes for internal use in libstdc++
2
2a60a9f6 3// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
c0736a9d 4// Free Software Foundation, Inc.
725dc051
BK
5//
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
748086b7 9// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
10// any later version.
11
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.
16
748086b7
JJ
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.
20
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/>.
725dc051 25
f910786b 26/** @file bits/cpp_type_traits.h
729e3d3f 27 * This is an internal header file, included by other library headers.
f910786b 28 * Do not attempt to use it directly. @headername{ext/type_traits}
729e3d3f
PE
29 */
30
143c27b0
BK
31// Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
32
3d7c150e
BK
33#ifndef _CPP_TYPE_TRAITS_H
34#define _CPP_TYPE_TRAITS_H 1
725dc051 35
b0a85b86
GDR
36#pragma GCC system_header
37
c0736a9d
PC
38#include <bits/c++config.h>
39
725dc051
BK
40//
41// This file provides some compile-time information about various types.
ca6c4418 42// These representations were designed, on purpose, to be constant-expressions
5e91e92e 43// and not types as found in <bits/type_traits.h>. In particular, they
725dc051
BK
44// can be used in control structures and the optimizer hopefully will do
45// the obvious thing.
46//
47// Why integral expressions, and not functions nor types?
ca6c4418
GDR
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
725dc051
BK
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
55// glance.
ca6c4418
GDR
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
725dc051
BK
58// keep these resource consumption as least as possible.
59//
ca6c4418
GDR
60// See valarray_array.h for a case use.
61//
725dc051
BK
62// -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
63//
c0736a9d
PC
64// Update 2005: types are also provided and <bits/type_traits.h> has been
65// removed.
66//
725dc051 67
5e91e92e 68// Forward declaration hack, should really include this from somewhere.
3cbc7af0
BK
69_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
70
5e91e92e
PC
71 template<typename _Iterator, typename _Container>
72 class __normal_iterator;
3cbc7af0
BK
73
74_GLIBCXX_END_NAMESPACE
5e91e92e 75
3cbc7af0
BK
76_GLIBCXX_BEGIN_NAMESPACE(std)
77
78a53887
BK
78 struct __true_type { };
79 struct __false_type { };
80
c0736a9d
PC
81 template<bool>
82 struct __truth_type
83 { typedef __false_type __type; };
84
85 template<>
86 struct __truth_type<true>
87 { typedef __true_type __type; };
88
a9dd5a46
PC
89 // N.B. The conversions to bool are needed due to the issue
90 // explained in c++/19404.
c0736a9d
PC
91 template<class _Sp, class _Tp>
92 struct __traitor
93 {
a9dd5a46 94 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
4d73fac9 95 typedef typename __truth_type<__value>::__type __type;
c0736a9d
PC
96 };
97
cdc958d8
GDR
98 // Compare for equality of types.
99 template<typename, typename>
100 struct __are_same
101 {
4d73fac9
PC
102 enum { __value = 0 };
103 typedef __false_type __type;
cdc958d8
GDR
104 };
105
106 template<typename _Tp>
107 struct __are_same<_Tp, _Tp>
108 {
4d73fac9
PC
109 enum { __value = 1 };
110 typedef __true_type __type;
cdc958d8
GDR
111 };
112
cdc958d8 113 // Holds if the template-argument is a void type.
725dc051 114 template<typename _Tp>
ca6c4418 115 struct __is_void
725dc051 116 {
4d73fac9 117 enum { __value = 0 };
c0736a9d 118 typedef __false_type __type;
725dc051 119 };
725dc051
BK
120
121 template<>
ca6c4418 122 struct __is_void<void>
725dc051 123 {
4d73fac9 124 enum { __value = 1 };
c0736a9d 125 typedef __true_type __type;
725dc051 126 };
725dc051
BK
127
128 //
129 // Integer types
130 //
131 template<typename _Tp>
132 struct __is_integer
133 {
4d73fac9 134 enum { __value = 0 };
c0736a9d 135 typedef __false_type __type;
725dc051
BK
136 };
137
138 // Thirteen specializations (yes there are eleven standard integer
2a60a9f6
BK
139 // types; <em>long long</em> and <em>unsigned long long</em> are
140 // supported as extensions)
725dc051
BK
141 template<>
142 struct __is_integer<bool>
143 {
4d73fac9 144 enum { __value = 1 };
c0736a9d 145 typedef __true_type __type;
725dc051 146 };
ed6814f7 147
725dc051
BK
148 template<>
149 struct __is_integer<char>
150 {
4d73fac9 151 enum { __value = 1 };
c0736a9d 152 typedef __true_type __type;
725dc051
BK
153 };
154
155 template<>
156 struct __is_integer<signed char>
157 {
4d73fac9 158 enum { __value = 1 };
c0736a9d 159 typedef __true_type __type;
725dc051 160 };
ed6814f7 161
725dc051 162 template<>
ff89096a 163 struct __is_integer<unsigned char>
725dc051 164 {
4d73fac9 165 enum { __value = 1 };
c0736a9d 166 typedef __true_type __type;
725dc051 167 };
725dc051 168
3d7c150e 169# ifdef _GLIBCXX_USE_WCHAR_T
725dc051 170 template<>
ff89096a 171 struct __is_integer<wchar_t>
725dc051 172 {
4d73fac9 173 enum { __value = 1 };
c0736a9d 174 typedef __true_type __type;
725dc051 175 };
725dc051 176# endif
ed6814f7 177
37f449aa
PC
178#ifdef __GXX_EXPERIMENTAL_CXX0X__
179 template<>
180 struct __is_integer<char16_t>
181 {
182 enum { __value = 1 };
183 typedef __true_type __type;
184 };
185
186 template<>
187 struct __is_integer<char32_t>
188 {
189 enum { __value = 1 };
190 typedef __true_type __type;
191 };
192#endif
193
725dc051 194 template<>
ff89096a 195 struct __is_integer<short>
725dc051 196 {
4d73fac9 197 enum { __value = 1 };
c0736a9d 198 typedef __true_type __type;
725dc051 199 };
725dc051
BK
200
201 template<>
ff89096a 202 struct __is_integer<unsigned short>
725dc051 203 {
4d73fac9 204 enum { __value = 1 };
c0736a9d 205 typedef __true_type __type;
725dc051 206 };
725dc051
BK
207
208 template<>
ff89096a 209 struct __is_integer<int>
725dc051 210 {
4d73fac9 211 enum { __value = 1 };
c0736a9d 212 typedef __true_type __type;
725dc051 213 };
725dc051
BK
214
215 template<>
ff89096a 216 struct __is_integer<unsigned int>
725dc051 217 {
4d73fac9 218 enum { __value = 1 };
c0736a9d 219 typedef __true_type __type;
725dc051 220 };
725dc051
BK
221
222 template<>
ff89096a 223 struct __is_integer<long>
725dc051 224 {
4d73fac9 225 enum { __value = 1 };
c0736a9d 226 typedef __true_type __type;
725dc051 227 };
725dc051
BK
228
229 template<>
ff89096a 230 struct __is_integer<unsigned long>
725dc051 231 {
4d73fac9 232 enum { __value = 1 };
c0736a9d 233 typedef __true_type __type;
725dc051 234 };
725dc051 235
725dc051 236 template<>
ff89096a 237 struct __is_integer<long long>
725dc051 238 {
4d73fac9 239 enum { __value = 1 };
c0736a9d 240 typedef __true_type __type;
725dc051 241 };
725dc051
BK
242
243 template<>
ff89096a 244 struct __is_integer<unsigned long long>
725dc051 245 {
4d73fac9 246 enum { __value = 1 };
c0736a9d 247 typedef __true_type __type;
725dc051 248 };
725dc051
BK
249
250 //
251 // Floating point types
252 //
253 template<typename _Tp>
ff89096a 254 struct __is_floating
725dc051 255 {
4d73fac9 256 enum { __value = 0 };
c0736a9d 257 typedef __false_type __type;
725dc051 258 };
725dc051
BK
259
260 // three specializations (float, double and 'long double')
261 template<>
ff89096a 262 struct __is_floating<float>
725dc051 263 {
4d73fac9 264 enum { __value = 1 };
c0736a9d 265 typedef __true_type __type;
725dc051 266 };
725dc051
BK
267
268 template<>
ff89096a 269 struct __is_floating<double>
725dc051 270 {
4d73fac9 271 enum { __value = 1 };
c0736a9d 272 typedef __true_type __type;
725dc051 273 };
725dc051
BK
274
275 template<>
ff89096a 276 struct __is_floating<long double>
725dc051 277 {
4d73fac9 278 enum { __value = 1 };
c0736a9d 279 typedef __true_type __type;
725dc051 280 };
725dc051 281
badd64ad
PC
282 //
283 // Pointer types
284 //
285 template<typename _Tp>
286 struct __is_pointer
287 {
4d73fac9 288 enum { __value = 0 };
c0736a9d 289 typedef __false_type __type;
badd64ad
PC
290 };
291
292 template<typename _Tp>
293 struct __is_pointer<_Tp*>
294 {
4d73fac9 295 enum { __value = 1 };
c0736a9d 296 typedef __true_type __type;
5e91e92e
PC
297 };
298
299 //
300 // Normal iterator type
301 //
302 template<typename _Tp>
303 struct __is_normal_iterator
304 {
4d73fac9 305 enum { __value = 0 };
c0736a9d 306 typedef __false_type __type;
5e91e92e
PC
307 };
308
309 template<typename _Iterator, typename _Container>
310 struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
311 _Container> >
312 {
4d73fac9 313 enum { __value = 1 };
c0736a9d 314 typedef __true_type __type;
badd64ad
PC
315 };
316
725dc051
BK
317 //
318 // An arithmetic type is an integer type or a floating point type
319 //
320 template<typename _Tp>
ff89096a 321 struct __is_arithmetic
c0736a9d
PC
322 : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
323 { };
324
725dc051
BK
325 //
326 // A fundamental type is `void' or and arithmetic type
327 //
328 template<typename _Tp>
ff89096a 329 struct __is_fundamental
c0736a9d
PC
330 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
331 { };
725dc051 332
badd64ad 333 //
67dd4a93 334 // A scalar type is an arithmetic type or a pointer type
badd64ad
PC
335 //
336 template<typename _Tp>
67dd4a93 337 struct __is_scalar
c0736a9d
PC
338 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
339 { };
badd64ad 340
0002d5d2
PC
341 //
342 // For use in std::copy and std::find overloads for streambuf iterators.
343 //
344 template<typename _Tp>
345 struct __is_char
346 {
347 enum { __value = 0 };
348 typedef __false_type __type;
349 };
350
351 template<>
352 struct __is_char<char>
353 {
354 enum { __value = 1 };
355 typedef __true_type __type;
356 };
357
358#ifdef _GLIBCXX_USE_WCHAR_T
359 template<>
360 struct __is_char<wchar_t>
361 {
362 enum { __value = 1 };
363 typedef __true_type __type;
364 };
365#endif
366
394033f8
PC
367 template<typename _Tp>
368 struct __is_byte
369 {
370 enum { __value = 0 };
371 typedef __false_type __type;
372 };
373
374 template<>
375 struct __is_byte<char>
376 {
377 enum { __value = 1 };
378 typedef __true_type __type;
379 };
380
381 template<>
382 struct __is_byte<signed char>
383 {
384 enum { __value = 1 };
385 typedef __true_type __type;
386 };
387
388 template<>
389 struct __is_byte<unsigned char>
390 {
391 enum { __value = 1 };
392 typedef __true_type __type;
393 };
394
f0112db9
PC
395 //
396 // Move iterator type
397 //
398 template<typename _Tp>
399 struct __is_move_iterator
400 {
401 enum { __value = 0 };
402 typedef __false_type __type;
403 };
404
405#ifdef __GXX_EXPERIMENTAL_CXX0X__
406 template<typename _Iterator>
407 class move_iterator;
408
409 template<typename _Iterator>
410 struct __is_move_iterator< move_iterator<_Iterator> >
411 {
412 enum { __value = 1 };
413 typedef __true_type __type;
414 };
415#endif
416
3cbc7af0 417_GLIBCXX_END_NAMESPACE
725dc051 418
3d7c150e 419#endif //_CPP_TYPE_TRAITS_H