1 // TR1 type_traits -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1_impl/type_traits
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
37 _GLIBCXX_BEGIN_NAMESPACE_TR1
39 // For use in __is_convertible_simple.
43 typedef struct { char __arr[2]; } __two;
46 #define _DEFINE_SPEC_BODY(_Value) \
47 : public integral_constant<bool, _Value> { };
49 #define _DEFINE_SPEC_0_HELPER(_Spec, _Value) \
52 _DEFINE_SPEC_BODY(_Value)
54 #define _DEFINE_SPEC_1_HELPER(_Spec, _Value) \
55 template<typename _Tp> \
57 _DEFINE_SPEC_BODY(_Value)
59 #define _DEFINE_SPEC_2_HELPER(_Spec, _Value) \
60 template<typename _Tp, typename _Cp> \
62 _DEFINE_SPEC_BODY(_Value)
64 #define _DEFINE_SPEC(_Order, _Trait, _Type, _Value) \
65 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type>, _Value) \
66 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type const>, _Value) \
67 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type volatile>, _Value) \
68 _DEFINE_SPEC_##_Order##_HELPER(_Trait<_Type const volatile>, _Value)
70 /// helper classes [4.3].
71 template<typename _Tp, _Tp __v>
72 struct integral_constant
74 static const _Tp value = __v;
75 typedef _Tp value_type;
76 typedef integral_constant<_Tp, __v> type;
79 /// typedef for true_type
80 typedef integral_constant<bool, true> true_type;
82 /// typedef for true_type
83 typedef integral_constant<bool, false> false_type;
85 template<typename _Tp, _Tp __v>
86 const _Tp integral_constant<_Tp, __v>::value;
88 /// primary type categories [4.5.1].
91 : public false_type { };
92 _DEFINE_SPEC(0, is_void, void, true)
97 : public false_type { };
98 _DEFINE_SPEC(0, is_integral, bool, true)
99 _DEFINE_SPEC(0, is_integral, char, true)
100 _DEFINE_SPEC(0, is_integral, signed char, true)
101 _DEFINE_SPEC(0, is_integral, unsigned char, true)
102 #ifdef _GLIBCXX_USE_WCHAR_T
103 _DEFINE_SPEC(0, is_integral, wchar_t, true)
105 _DEFINE_SPEC(0, is_integral, short, true)
106 _DEFINE_SPEC(0, is_integral, unsigned short, true)
107 _DEFINE_SPEC(0, is_integral, int, true)
108 _DEFINE_SPEC(0, is_integral, unsigned int, true)
109 _DEFINE_SPEC(0, is_integral, long, true)
110 _DEFINE_SPEC(0, is_integral, unsigned long, true)
111 _DEFINE_SPEC(0, is_integral, long long, true)
112 _DEFINE_SPEC(0, is_integral, unsigned long long, true)
114 /// is_floating_point
116 struct is_floating_point
117 : public false_type { };
118 _DEFINE_SPEC(0, is_floating_point, float, true)
119 _DEFINE_SPEC(0, is_floating_point, double, true)
120 _DEFINE_SPEC(0, is_floating_point, long double, true)
125 : public false_type { };
127 template<typename _Tp, std::size_t _Size>
128 struct is_array<_Tp[_Size]>
129 : public true_type { };
131 template<typename _Tp>
132 struct is_array<_Tp[]>
133 : public true_type { };
138 : public false_type { };
139 _DEFINE_SPEC(1, is_pointer, _Tp*, true)
142 template<typename _Tp>
146 template<typename _Tp>
149 /// is_member_object_pointer
151 struct is_member_object_pointer
152 : public false_type { };
153 _DEFINE_SPEC(2, is_member_object_pointer, _Tp _Cp::*,
154 !is_function<_Tp>::value)
156 /// is_member_function_pointer
158 struct is_member_function_pointer
159 : public false_type { };
160 _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*,
161 is_function<_Tp>::value)
164 template<typename _Tp>
166 : public integral_constant<bool, __is_enum(_Tp)>
170 template<typename _Tp>
172 : public integral_constant<bool, __is_union(_Tp)>
176 template<typename _Tp>
178 : public integral_constant<bool, __is_class(_Tp)>
181 template<typename _Tp>
183 : public __sfinae_types
186 template<typename _Up>
187 static __one __test(_Up(*)[1]);
189 static __two __test(...);
192 static const bool __value = sizeof(__test<_Tp>(0)) == 1;
196 template<typename _Tp>
200 template<typename _Tp>
202 : public integral_constant<bool, !(__in_array<_Tp>::__value
203 || is_abstract<_Tp>::value
204 || is_reference<_Tp>::value
205 || is_void<_Tp>::value)>
208 /// composite type traits [4.5.2].
209 template<typename _Tp>
211 : public integral_constant<bool, (is_integral<_Tp>::value
212 || is_floating_point<_Tp>::value)>
216 template<typename _Tp>
217 struct is_fundamental
218 : public integral_constant<bool, (is_arithmetic<_Tp>::value
219 || is_void<_Tp>::value)>
223 template<typename _Tp>
225 : public integral_constant<bool, !(is_function<_Tp>::value
226 || is_reference<_Tp>::value
227 || is_void<_Tp>::value)>
230 /// is_member_pointer
231 template<typename _Tp>
232 struct is_member_pointer;
235 template<typename _Tp>
237 : public integral_constant<bool, (is_arithmetic<_Tp>::value
238 || is_enum<_Tp>::value
239 || is_pointer<_Tp>::value
240 || is_member_pointer<_Tp>::value)>
244 template<typename _Tp>
246 : public integral_constant<bool, !is_fundamental<_Tp>::value> { };
248 /// is_member_pointer
249 template<typename _Tp>
250 struct is_member_pointer
251 : public integral_constant<bool,
252 (is_member_object_pointer<_Tp>::value
253 || is_member_function_pointer<_Tp>::value)>
256 /// type properties [4.5.3].
259 : public false_type { };
262 template<typename _Tp>
263 struct is_const<_Tp const>
264 : public true_type { };
269 : public false_type { };
271 template<typename _Tp>
272 struct is_volatile<_Tp volatile>
273 : public true_type { };
276 template<typename _Tp>
278 : public integral_constant<bool, __is_empty(_Tp)>
282 template<typename _Tp>
283 struct is_polymorphic
284 : public integral_constant<bool, __is_polymorphic(_Tp)>
288 template<typename _Tp>
290 : public integral_constant<bool, __is_abstract(_Tp)>
293 /// has_virtual_destructor
294 template<typename _Tp>
295 struct has_virtual_destructor
296 : public integral_constant<bool, __has_virtual_destructor(_Tp)>
300 template<typename _Tp>
302 : public integral_constant<std::size_t, __alignof__(_Tp)> { };
307 : public integral_constant<std::size_t, 0> { };
309 template<typename _Tp, std::size_t _Size>
310 struct rank<_Tp[_Size]>
311 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
313 template<typename _Tp>
315 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
318 template<typename, unsigned _Uint = 0>
320 : public integral_constant<std::size_t, 0> { };
322 template<typename _Tp, unsigned _Uint, std::size_t _Size>
323 struct extent<_Tp[_Size], _Uint>
324 : public integral_constant<std::size_t,
325 _Uint == 0 ? _Size : extent<_Tp,
329 template<typename _Tp, unsigned _Uint>
330 struct extent<_Tp[], _Uint>
331 : public integral_constant<std::size_t,
332 _Uint == 0 ? 0 : extent<_Tp,
336 /// relationships between types [4.6].
337 template<typename, typename>
339 : public false_type { };
341 template<typename _Tp>
342 struct is_same<_Tp, _Tp>
343 : public true_type { };
345 /// const-volatile modifications [4.7.1].
346 template<typename _Tp>
348 { typedef _Tp type; };
350 template<typename _Tp>
351 struct remove_const<_Tp const>
352 { typedef _Tp type; };
355 template<typename _Tp>
356 struct remove_volatile
357 { typedef _Tp type; };
359 template<typename _Tp>
360 struct remove_volatile<_Tp volatile>
361 { typedef _Tp type; };
364 template<typename _Tp>
368 remove_const<typename remove_volatile<_Tp>::type>::type type;
372 template<typename _Tp>
374 { typedef _Tp const type; };
377 template<typename _Tp>
379 { typedef _Tp volatile type; };
382 template<typename _Tp>
386 add_const<typename add_volatile<_Tp>::type>::type type;
389 /// array modifications [4.7.3].
390 template<typename _Tp>
392 { typedef _Tp type; };
394 template<typename _Tp, std::size_t _Size>
395 struct remove_extent<_Tp[_Size]>
396 { typedef _Tp type; };
398 template<typename _Tp>
399 struct remove_extent<_Tp[]>
400 { typedef _Tp type; };
402 /// remove_all_extents
403 template<typename _Tp>
404 struct remove_all_extents
405 { typedef _Tp type; };
407 template<typename _Tp, std::size_t _Size>
408 struct remove_all_extents<_Tp[_Size]>
409 { typedef typename remove_all_extents<_Tp>::type type; };
411 template<typename _Tp>
412 struct remove_all_extents<_Tp[]>
413 { typedef typename remove_all_extents<_Tp>::type type; };
415 /// pointer modifications [4.7.4].
416 #undef _DEFINE_SPEC_BODY
417 #define _DEFINE_SPEC_BODY(_Value) \
418 { typedef _Tp type; };
421 template<typename _Tp>
422 struct remove_pointer
423 { typedef _Tp type; };
424 _DEFINE_SPEC(1, remove_pointer, _Tp*, false)
427 template<typename _Tp>
428 struct remove_reference;
431 template<typename _Tp>
433 { typedef typename remove_reference<_Tp>::type* type; };
435 #undef _DEFINE_SPEC_0_HELPER
436 #undef _DEFINE_SPEC_1_HELPER
437 #undef _DEFINE_SPEC_2_HELPER
439 #undef _DEFINE_SPEC_BODY
441 _GLIBCXX_END_NAMESPACE_TR1