]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/cpp_type_traits.h
c++config: Add in revised namespace associations.
[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
c0736a9d
PC
3// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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
9// Free Software Foundation; either version 2, or (at your option)
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
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
83f51799 19// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
725dc051
BK
20// USA.
21
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.
30
31// Written by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
32
729e3d3f
PE
33/** @file cpp_type_traits.h
34 * This is an internal header file, included by other library headers.
35 * You should not attempt to use it directly.
36 */
37
3d7c150e
BK
38#ifndef _CPP_TYPE_TRAITS_H
39#define _CPP_TYPE_TRAITS_H 1
725dc051 40
b0a85b86
GDR
41#pragma GCC system_header
42
c0736a9d
PC
43#include <bits/c++config.h>
44
725dc051
BK
45//
46// This file provides some compile-time information about various types.
ca6c4418 47// These representations were designed, on purpose, to be constant-expressions
5e91e92e 48// and not types as found in <bits/type_traits.h>. In particular, they
725dc051
BK
49// can be used in control structures and the optimizer hopefully will do
50// the obvious thing.
51//
52// Why integral expressions, and not functions nor types?
ca6c4418
GDR
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
725dc051
BK
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
60// glance.
ca6c4418
GDR
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
725dc051
BK
63// keep these resource consumption as least as possible.
64//
ca6c4418
GDR
65// See valarray_array.h for a case use.
66//
725dc051
BK
67// -- Gaby (dosreis@cmla.ens-cachan.fr) 2000-03-06.
68//
c0736a9d
PC
69// Update 2005: types are also provided and <bits/type_traits.h> has been
70// removed.
71//
725dc051 72
ff89096a
PC
73// NB: g++ can not compile these if declared within the class
74// __is_pod itself.
75namespace __gnu_internal
76{
77 typedef char __one;
78 typedef char __two[2];
79
4d73fac9
PC
80 template<typename _Tp>
81 __one __test_type(int _Tp::*);
82 template<typename _Tp>
83 __two& __test_type(...);
ff89096a
PC
84} // namespace __gnu_internal
85
5e91e92e 86// Forward declaration hack, should really include this from somewhere.
3cbc7af0
BK
87_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
88
5e91e92e
PC
89 template<typename _Iterator, typename _Container>
90 class __normal_iterator;
3cbc7af0
BK
91
92_GLIBCXX_END_NAMESPACE
5e91e92e 93
c0736a9d
PC
94struct __true_type { };
95struct __false_type { };
96
3cbc7af0
BK
97_GLIBCXX_BEGIN_NAMESPACE(std)
98
c0736a9d
PC
99 template<bool>
100 struct __truth_type
101 { typedef __false_type __type; };
102
103 template<>
104 struct __truth_type<true>
105 { typedef __true_type __type; };
106
a9dd5a46
PC
107 // N.B. The conversions to bool are needed due to the issue
108 // explained in c++/19404.
c0736a9d
PC
109 template<class _Sp, class _Tp>
110 struct __traitor
111 {
a9dd5a46 112 enum { __value = bool(_Sp::__value) || bool(_Tp::__value) };
4d73fac9 113 typedef typename __truth_type<__value>::__type __type;
c0736a9d
PC
114 };
115
cdc958d8
GDR
116 // Compare for equality of types.
117 template<typename, typename>
118 struct __are_same
119 {
4d73fac9
PC
120 enum { __value = 0 };
121 typedef __false_type __type;
cdc958d8
GDR
122 };
123
124 template<typename _Tp>
125 struct __are_same<_Tp, _Tp>
126 {
4d73fac9
PC
127 enum { __value = 1 };
128 typedef __true_type __type;
cdc958d8
GDR
129 };
130
131 // Define a nested type if some predicate holds.
132 template<typename, bool>
133 struct __enable_if
c0736a9d 134 {
cdc958d8
GDR
135 };
136
137 template<typename _Tp>
ff89096a 138 struct __enable_if<_Tp, true>
cdc958d8 139 {
4d73fac9 140 typedef _Tp __type;
cdc958d8
GDR
141 };
142
143 // Holds if the template-argument is a void type.
725dc051 144 template<typename _Tp>
ca6c4418 145 struct __is_void
725dc051 146 {
4d73fac9 147 enum { __value = 0 };
c0736a9d 148 typedef __false_type __type;
725dc051 149 };
725dc051
BK
150
151 template<>
ca6c4418 152 struct __is_void<void>
725dc051 153 {
4d73fac9 154 enum { __value = 1 };
c0736a9d 155 typedef __true_type __type;
725dc051 156 };
725dc051
BK
157
158 //
159 // Integer types
160 //
161 template<typename _Tp>
162 struct __is_integer
163 {
4d73fac9 164 enum { __value = 0 };
c0736a9d 165 typedef __false_type __type;
725dc051
BK
166 };
167
168 // Thirteen specializations (yes there are eleven standard integer
169 // types; 'long long' and 'unsigned long long' are supported as
170 // extensions)
171 template<>
172 struct __is_integer<bool>
173 {
4d73fac9 174 enum { __value = 1 };
c0736a9d 175 typedef __true_type __type;
725dc051 176 };
ed6814f7 177
725dc051
BK
178 template<>
179 struct __is_integer<char>
180 {
4d73fac9 181 enum { __value = 1 };
c0736a9d 182 typedef __true_type __type;
725dc051
BK
183 };
184
185 template<>
186 struct __is_integer<signed char>
187 {
4d73fac9 188 enum { __value = 1 };
c0736a9d 189 typedef __true_type __type;
725dc051 190 };
ed6814f7 191
725dc051 192 template<>
ff89096a 193 struct __is_integer<unsigned char>
725dc051 194 {
4d73fac9 195 enum { __value = 1 };
c0736a9d 196 typedef __true_type __type;
725dc051 197 };
725dc051 198
3d7c150e 199# ifdef _GLIBCXX_USE_WCHAR_T
725dc051 200 template<>
ff89096a 201 struct __is_integer<wchar_t>
725dc051 202 {
4d73fac9 203 enum { __value = 1 };
c0736a9d 204 typedef __true_type __type;
725dc051 205 };
725dc051 206# endif
ed6814f7 207
725dc051 208 template<>
ff89096a 209 struct __is_integer<short>
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 short>
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<int>
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 int>
725dc051 231 {
4d73fac9 232 enum { __value = 1 };
c0736a9d 233 typedef __true_type __type;
725dc051 234 };
725dc051
BK
235
236 template<>
ff89096a 237 struct __is_integer<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>
725dc051 245 {
4d73fac9 246 enum { __value = 1 };
c0736a9d 247 typedef __true_type __type;
725dc051 248 };
725dc051 249
725dc051 250 template<>
ff89096a 251 struct __is_integer<long long>
725dc051 252 {
4d73fac9 253 enum { __value = 1 };
c0736a9d 254 typedef __true_type __type;
725dc051 255 };
725dc051
BK
256
257 template<>
ff89096a 258 struct __is_integer<unsigned long long>
725dc051 259 {
4d73fac9 260 enum { __value = 1 };
c0736a9d 261 typedef __true_type __type;
725dc051 262 };
725dc051
BK
263
264 //
265 // Floating point types
266 //
267 template<typename _Tp>
ff89096a 268 struct __is_floating
725dc051 269 {
4d73fac9 270 enum { __value = 0 };
c0736a9d 271 typedef __false_type __type;
725dc051 272 };
725dc051
BK
273
274 // three specializations (float, double and 'long double')
275 template<>
ff89096a 276 struct __is_floating<float>
725dc051 277 {
4d73fac9 278 enum { __value = 1 };
c0736a9d 279 typedef __true_type __type;
725dc051 280 };
725dc051
BK
281
282 template<>
ff89096a 283 struct __is_floating<double>
725dc051 284 {
4d73fac9 285 enum { __value = 1 };
c0736a9d 286 typedef __true_type __type;
725dc051 287 };
725dc051
BK
288
289 template<>
ff89096a 290 struct __is_floating<long double>
725dc051 291 {
4d73fac9 292 enum { __value = 1 };
c0736a9d 293 typedef __true_type __type;
725dc051 294 };
725dc051 295
badd64ad
PC
296 //
297 // Pointer types
298 //
299 template<typename _Tp>
300 struct __is_pointer
301 {
4d73fac9 302 enum { __value = 0 };
c0736a9d 303 typedef __false_type __type;
badd64ad
PC
304 };
305
306 template<typename _Tp>
307 struct __is_pointer<_Tp*>
308 {
4d73fac9 309 enum { __value = 1 };
c0736a9d 310 typedef __true_type __type;
5e91e92e
PC
311 };
312
313 //
314 // Normal iterator type
315 //
316 template<typename _Tp>
317 struct __is_normal_iterator
318 {
4d73fac9 319 enum { __value = 0 };
c0736a9d 320 typedef __false_type __type;
5e91e92e
PC
321 };
322
323 template<typename _Iterator, typename _Container>
324 struct __is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
325 _Container> >
326 {
4d73fac9 327 enum { __value = 1 };
c0736a9d 328 typedef __true_type __type;
badd64ad
PC
329 };
330
725dc051
BK
331 //
332 // An arithmetic type is an integer type or a floating point type
333 //
334 template<typename _Tp>
ff89096a 335 struct __is_arithmetic
c0736a9d
PC
336 : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
337 { };
338
725dc051
BK
339 //
340 // A fundamental type is `void' or and arithmetic type
341 //
342 template<typename _Tp>
ff89096a 343 struct __is_fundamental
c0736a9d
PC
344 : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
345 { };
725dc051 346
badd64ad 347 //
67dd4a93 348 // A scalar type is an arithmetic type or a pointer type
badd64ad
PC
349 //
350 template<typename _Tp>
67dd4a93 351 struct __is_scalar
c0736a9d
PC
352 : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
353 { };
badd64ad 354
725dc051
BK
355 //
356 // For the immediate use, the following is a good approximation
357 //
358 template<typename _Tp>
ff89096a 359 struct __is_pod
725dc051 360 {
ff89096a
PC
361 enum
362 {
4d73fac9 363 __value = (sizeof(__gnu_internal::__test_type<_Tp>(0))
ff89096a
PC
364 != sizeof(__gnu_internal::__one))
365 };
725dc051 366 };
725dc051 367
7697e6c6
PC
368 //
369 // A stripped-down version of std::tr1::is_empty
370 //
371 template<typename _Tp>
372 struct __is_empty
373 {
374 private:
375 template<typename>
376 struct __first { };
377 template<typename _Up>
378 struct __second
379 : public _Up { };
380
381 public:
382 enum
383 {
384 __value = sizeof(__first<_Tp>) == sizeof(__second<_Tp>)
385 };
386 };
387
3cbc7af0 388_GLIBCXX_END_NAMESPACE
725dc051 389
3d7c150e 390#endif //_CPP_TYPE_TRAITS_H