1 // TR1 type_traits -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 3, 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 // 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.
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/>.
26 /** @file tr1/type_traits
27 * This is a TR1 C++ Library header.
30 #ifndef _GLIBCXX_TR1_TYPE_TRAITS
31 #define _GLIBCXX_TR1_TYPE_TRAITS 1
33 #pragma GCC system_header
35 #include <bits/c++config.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @addtogroup metaprogramming
51 typedef struct { char __arr[2]; } __two;
54 #define _DEFINE_SPEC_0_HELPER \
57 #define _DEFINE_SPEC_1_HELPER \
58 template<typename _Tp>
60 #define _DEFINE_SPEC_2_HELPER \
61 template<typename _Tp, typename _Cp>
63 #define _DEFINE_SPEC(_Order, _Trait, _Type, _Value) \
64 _DEFINE_SPEC_##_Order##_HELPER \
65 struct _Trait<_Type> \
66 : public integral_constant<bool, _Value> { };
68 // 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 false_type
83 typedef integral_constant<bool, false> false_type;
85 template<typename _Tp, _Tp __v>
86 const _Tp integral_constant<_Tp, __v>::value;
93 struct __is_void_helper
94 : public false_type { };
95 _DEFINE_SPEC(0, __is_void_helper, void, true)
97 // primary type categories [4.5.1].
100 template<typename _Tp>
102 : public integral_constant<bool, (__is_void_helper<typename
103 remove_cv<_Tp>::type>::value)>
107 struct __is_integral_helper
108 : public false_type { };
109 _DEFINE_SPEC(0, __is_integral_helper, bool, true)
110 _DEFINE_SPEC(0, __is_integral_helper, char, true)
111 _DEFINE_SPEC(0, __is_integral_helper, signed char, true)
112 _DEFINE_SPEC(0, __is_integral_helper, unsigned char, true)
113 #ifdef _GLIBCXX_USE_WCHAR_T
114 _DEFINE_SPEC(0, __is_integral_helper, wchar_t, true)
116 _DEFINE_SPEC(0, __is_integral_helper, short, true)
117 _DEFINE_SPEC(0, __is_integral_helper, unsigned short, true)
118 _DEFINE_SPEC(0, __is_integral_helper, int, true)
119 _DEFINE_SPEC(0, __is_integral_helper, unsigned int, true)
120 _DEFINE_SPEC(0, __is_integral_helper, long, true)
121 _DEFINE_SPEC(0, __is_integral_helper, unsigned long, true)
122 _DEFINE_SPEC(0, __is_integral_helper, long long, true)
123 _DEFINE_SPEC(0, __is_integral_helper, unsigned long long, true)
126 template<typename _Tp>
128 : public integral_constant<bool, (__is_integral_helper<typename
129 remove_cv<_Tp>::type>::value)>
133 struct __is_floating_point_helper
134 : public false_type { };
135 _DEFINE_SPEC(0, __is_floating_point_helper, float, true)
136 _DEFINE_SPEC(0, __is_floating_point_helper, double, true)
137 _DEFINE_SPEC(0, __is_floating_point_helper, long double, true)
139 /// is_floating_point
140 template<typename _Tp>
141 struct is_floating_point
142 : public integral_constant<bool, (__is_floating_point_helper<typename
143 remove_cv<_Tp>::type>::value)>
149 : public false_type { };
151 template<typename _Tp, std::size_t _Size>
152 struct is_array<_Tp[_Size]>
153 : public true_type { };
155 template<typename _Tp>
156 struct is_array<_Tp[]>
157 : public true_type { };
160 struct __is_pointer_helper
161 : public false_type { };
162 _DEFINE_SPEC(1, __is_pointer_helper, _Tp*, true)
165 template<typename _Tp>
167 : public integral_constant<bool, (__is_pointer_helper<typename
168 remove_cv<_Tp>::type>::value)>
172 template<typename _Tp>
176 template<typename _Tp>
180 struct __is_member_object_pointer_helper
181 : public false_type { };
182 _DEFINE_SPEC(2, __is_member_object_pointer_helper, _Tp _Cp::*,
183 !is_function<_Tp>::value)
185 /// is_member_object_pointer
186 template<typename _Tp>
187 struct is_member_object_pointer
188 : public integral_constant<bool, (__is_member_object_pointer_helper<
189 typename remove_cv<_Tp>::type>::value)>
193 struct __is_member_function_pointer_helper
194 : public false_type { };
195 _DEFINE_SPEC(2, __is_member_function_pointer_helper, _Tp _Cp::*,
196 is_function<_Tp>::value)
198 /// is_member_function_pointer
199 template<typename _Tp>
200 struct is_member_function_pointer
201 : public integral_constant<bool, (__is_member_function_pointer_helper<
202 typename remove_cv<_Tp>::type>::value)>
206 template<typename _Tp>
208 : public integral_constant<bool, __is_enum(_Tp)>
212 template<typename _Tp>
214 : public integral_constant<bool, __is_union(_Tp)>
218 template<typename _Tp>
220 : public integral_constant<bool, __is_class(_Tp)>
226 : public false_type { };
227 template<typename _Res, typename... _ArgTypes>
228 struct is_function<_Res(_ArgTypes...)>
229 : public true_type { };
230 template<typename _Res, typename... _ArgTypes>
231 struct is_function<_Res(_ArgTypes......)>
232 : public true_type { };
233 template<typename _Res, typename... _ArgTypes>
234 struct is_function<_Res(_ArgTypes...) const>
235 : public true_type { };
236 template<typename _Res, typename... _ArgTypes>
237 struct is_function<_Res(_ArgTypes......) const>
238 : public true_type { };
239 template<typename _Res, typename... _ArgTypes>
240 struct is_function<_Res(_ArgTypes...) volatile>
241 : public true_type { };
242 template<typename _Res, typename... _ArgTypes>
243 struct is_function<_Res(_ArgTypes......) volatile>
244 : public true_type { };
245 template<typename _Res, typename... _ArgTypes>
246 struct is_function<_Res(_ArgTypes...) const volatile>
247 : public true_type { };
248 template<typename _Res, typename... _ArgTypes>
249 struct is_function<_Res(_ArgTypes......) const volatile>
250 : public true_type { };
252 // composite type traits [4.5.2].
255 template<typename _Tp>
257 : public integral_constant<bool, (is_integral<_Tp>::value
258 || is_floating_point<_Tp>::value)>
262 template<typename _Tp>
263 struct is_fundamental
264 : public integral_constant<bool, (is_arithmetic<_Tp>::value
265 || is_void<_Tp>::value)>
269 template<typename _Tp>
271 : public integral_constant<bool, !(is_function<_Tp>::value
272 || is_reference<_Tp>::value
273 || is_void<_Tp>::value)>
276 /// is_member_pointer
277 template<typename _Tp>
278 struct is_member_pointer;
281 template<typename _Tp>
283 : public integral_constant<bool, (is_arithmetic<_Tp>::value
284 || is_enum<_Tp>::value
285 || is_pointer<_Tp>::value
286 || is_member_pointer<_Tp>::value)>
290 template<typename _Tp>
292 : public integral_constant<bool, !is_fundamental<_Tp>::value> { };
294 /// is_member_pointer
295 template<typename _Tp>
296 struct __is_member_pointer_helper
297 : public false_type { };
298 _DEFINE_SPEC(2, __is_member_pointer_helper, _Tp _Cp::*, true)
300 template<typename _Tp>
301 struct is_member_pointer
302 : public integral_constant<bool, (__is_member_pointer_helper<
303 typename remove_cv<_Tp>::type>::value)>
306 // type properties [4.5.3].
310 : public false_type { };
312 template<typename _Tp>
313 struct is_const<_Tp const>
314 : public true_type { };
319 : public false_type { };
321 template<typename _Tp>
322 struct is_volatile<_Tp volatile>
323 : public true_type { };
326 template<typename _Tp>
328 : public integral_constant<bool, __is_empty(_Tp)>
332 template<typename _Tp>
333 struct is_polymorphic
334 : public integral_constant<bool, __is_polymorphic(_Tp)>
338 template<typename _Tp>
340 : public integral_constant<bool, __is_abstract(_Tp)>
343 /// has_virtual_destructor
344 template<typename _Tp>
345 struct has_virtual_destructor
346 : public integral_constant<bool, __has_virtual_destructor(_Tp)>
350 template<typename _Tp>
352 : public integral_constant<std::size_t, __alignof__(_Tp)> { };
357 : public integral_constant<std::size_t, 0> { };
359 template<typename _Tp, std::size_t _Size>
360 struct rank<_Tp[_Size]>
361 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
363 template<typename _Tp>
365 : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { };
368 template<typename, unsigned _Uint = 0>
370 : public integral_constant<std::size_t, 0> { };
372 template<typename _Tp, unsigned _Uint, std::size_t _Size>
373 struct extent<_Tp[_Size], _Uint>
374 : public integral_constant<std::size_t,
375 _Uint == 0 ? _Size : extent<_Tp,
379 template<typename _Tp, unsigned _Uint>
380 struct extent<_Tp[], _Uint>
381 : public integral_constant<std::size_t,
382 _Uint == 0 ? 0 : extent<_Tp,
386 // relationships between types [4.6].
389 template<typename, typename>
391 : public false_type { };
393 template<typename _Tp>
394 struct is_same<_Tp, _Tp>
395 : public true_type { };
397 // const-volatile modifications [4.7.1].
400 template<typename _Tp>
402 { typedef _Tp type; };
404 template<typename _Tp>
405 struct remove_const<_Tp const>
406 { typedef _Tp type; };
409 template<typename _Tp>
410 struct remove_volatile
411 { typedef _Tp type; };
413 template<typename _Tp>
414 struct remove_volatile<_Tp volatile>
415 { typedef _Tp type; };
418 template<typename _Tp>
422 remove_const<typename remove_volatile<_Tp>::type>::type type;
426 template<typename _Tp>
428 { typedef _Tp const type; };
431 template<typename _Tp>
433 { typedef _Tp volatile type; };
436 template<typename _Tp>
440 add_const<typename add_volatile<_Tp>::type>::type type;
443 // array modifications [4.7.3].
446 template<typename _Tp>
448 { typedef _Tp type; };
450 template<typename _Tp, std::size_t _Size>
451 struct remove_extent<_Tp[_Size]>
452 { typedef _Tp type; };
454 template<typename _Tp>
455 struct remove_extent<_Tp[]>
456 { typedef _Tp type; };
458 /// remove_all_extents
459 template<typename _Tp>
460 struct remove_all_extents
461 { typedef _Tp type; };
463 template<typename _Tp, std::size_t _Size>
464 struct remove_all_extents<_Tp[_Size]>
465 { typedef typename remove_all_extents<_Tp>::type type; };
467 template<typename _Tp>
468 struct remove_all_extents<_Tp[]>
469 { typedef typename remove_all_extents<_Tp>::type type; };
471 // pointer modifications [4.7.4].
473 template<typename _Tp, typename>
474 struct __remove_pointer_helper
475 { typedef _Tp type; };
477 template<typename _Tp, typename _Up>
478 struct __remove_pointer_helper<_Tp, _Up*>
479 { typedef _Up type; };
482 template<typename _Tp>
483 struct remove_pointer
484 : public __remove_pointer_helper<_Tp, typename remove_cv<_Tp>::type>
488 struct remove_reference;
491 template<typename _Tp>
493 { typedef typename remove_reference<_Tp>::type* type; };
497 : public false_type { };
499 template<typename _Tp>
500 struct is_reference<_Tp&>
501 : public true_type { };
503 template<typename _Tp>
505 : public integral_constant<bool, __is_pod(_Tp) || is_void<_Tp>::value>
508 template<typename _Tp>
509 struct has_trivial_constructor
510 : public integral_constant<bool, is_pod<_Tp>::value>
513 template<typename _Tp>
514 struct has_trivial_copy
515 : public integral_constant<bool, is_pod<_Tp>::value>
518 template<typename _Tp>
519 struct has_trivial_assign
520 : public integral_constant<bool, is_pod<_Tp>::value>
523 template<typename _Tp>
524 struct has_trivial_destructor
525 : public integral_constant<bool, is_pod<_Tp>::value>
528 template<typename _Tp>
529 struct has_nothrow_constructor
530 : public integral_constant<bool, is_pod<_Tp>::value>
533 template<typename _Tp>
534 struct has_nothrow_copy
535 : public integral_constant<bool, is_pod<_Tp>::value>
538 template<typename _Tp>
539 struct has_nothrow_assign
540 : public integral_constant<bool, is_pod<_Tp>::value>
544 struct __is_signed_helper
545 : public false_type { };
546 _DEFINE_SPEC(0, __is_signed_helper, signed char, true)
547 _DEFINE_SPEC(0, __is_signed_helper, short, true)
548 _DEFINE_SPEC(0, __is_signed_helper, int, true)
549 _DEFINE_SPEC(0, __is_signed_helper, long, true)
550 _DEFINE_SPEC(0, __is_signed_helper, long long, true)
552 template<typename _Tp>
554 : public integral_constant<bool, (__is_signed_helper<typename
555 remove_cv<_Tp>::type>::value)>
559 struct __is_unsigned_helper
560 : public false_type { };
561 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned char, true)
562 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned short, true)
563 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned int, true)
564 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned long, true)
565 _DEFINE_SPEC(0, __is_unsigned_helper, unsigned long long, true)
567 template<typename _Tp>
569 : public integral_constant<bool, (__is_unsigned_helper<typename
570 remove_cv<_Tp>::type>::value)>
573 template<typename _Base, typename _Derived>
574 struct __is_base_of_helper
576 typedef typename remove_cv<_Base>::type _NoCv_Base;
577 typedef typename remove_cv<_Derived>::type _NoCv_Derived;
578 static const bool __value = (is_same<_Base, _Derived>::value
579 || (__is_base_of(_Base, _Derived)
580 && !is_same<_NoCv_Base,
581 _NoCv_Derived>::value));
584 template<typename _Base, typename _Derived>
586 : public integral_constant<bool,
587 __is_base_of_helper<_Base, _Derived>::__value>
590 template<typename _From, typename _To>
591 struct __is_convertible_simple
592 : public __sfinae_types
595 static __one __test(_To);
596 static __two __test(...);
597 static _From __makeFrom();
600 static const bool __value = sizeof(__test(__makeFrom())) == 1;
603 template<typename _Tp>
604 struct add_reference;
606 template<typename _Tp>
607 struct __is_int_or_cref
609 typedef typename remove_reference<_Tp>::type __rr_Tp;
610 static const bool __value = (is_integral<_Tp>::value
611 || (is_integral<__rr_Tp>::value
612 && is_const<__rr_Tp>::value
613 && !is_volatile<__rr_Tp>::value));
616 template<typename _From, typename _To,
617 bool = (is_void<_From>::value || is_void<_To>::value
618 || is_function<_To>::value || is_array<_To>::value
619 // This special case is here only to avoid warnings.
620 || (is_floating_point<typename
621 remove_reference<_From>::type>::value
622 && __is_int_or_cref<_To>::__value))>
623 struct __is_convertible_helper
625 // "An imaginary lvalue of type From...".
626 static const bool __value = (__is_convertible_simple<typename
627 add_reference<_From>::type, _To>::__value);
630 template<typename _From, typename _To>
631 struct __is_convertible_helper<_From, _To, true>
632 { static const bool __value = (is_void<_To>::value
633 || (__is_int_or_cref<_To>::__value
634 && !is_void<_From>::value)); };
636 template<typename _From, typename _To>
637 struct is_convertible
638 : public integral_constant<bool,
639 __is_convertible_helper<_From, _To>::__value>
642 // reference modifications [4.7.2].
643 template<typename _Tp>
644 struct remove_reference
645 { typedef _Tp type; };
647 template<typename _Tp>
648 struct remove_reference<_Tp&>
649 { typedef _Tp type; };
651 // NB: Careful with reference to void.
652 template<typename _Tp, bool = (is_void<_Tp>::value
653 || is_reference<_Tp>::value)>
654 struct __add_reference_helper
655 { typedef _Tp& type; };
657 template<typename _Tp>
658 struct __add_reference_helper<_Tp, true>
659 { typedef _Tp type; };
661 template<typename _Tp>
663 : public __add_reference_helper<_Tp>
666 // other transformations [4.8].
667 template<std::size_t _Len, std::size_t _Align>
668 struct aligned_storage
672 unsigned char __data[_Len];
673 struct __attribute__((__aligned__((_Align)))) { } __align;
677 #undef _DEFINE_SPEC_0_HELPER
678 #undef _DEFINE_SPEC_1_HELPER
679 #undef _DEFINE_SPEC_2_HELPER
682 /// @} group metaprogramming
684 _GLIBCXX_END_NAMESPACE_VERSION
688 #endif // _GLIBCXX_TR1_TYPE_TRAITS