// C++0x type_traits -*- C++ -*- // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation. // You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // . /** @file include/type_traits * This is a Standard C++ Library header. */ #ifndef _GLIBCXX_TYPE_TRAITS #define _GLIBCXX_TYPE_TRAITS 1 #pragma GCC system_header #ifndef __GXX_EXPERIMENTAL_CXX0X__ # include #else #include namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @addtogroup metaprogramming * @{ */ struct __sfinae_types { typedef char __one; typedef struct { char __arr[2]; } __two; }; #define _DEFINE_SPEC_0_HELPER \ template<> #define _DEFINE_SPEC_1_HELPER \ template #define _DEFINE_SPEC_2_HELPER \ template #define _DEFINE_SPEC(_Order, _Trait, _Type, _Value) \ _DEFINE_SPEC_##_Order##_HELPER \ struct _Trait<_Type> \ : public integral_constant { }; // helper classes. /// integral_constant template struct integral_constant { static constexpr _Tp value = __v; typedef _Tp value_type; typedef integral_constant<_Tp, __v> type; constexpr operator value_type() { return value; } }; /// typedef for true_type typedef integral_constant true_type; /// typedef for false_type typedef integral_constant false_type; template constexpr _Tp integral_constant<_Tp, __v>::value; /// remove_cv template struct remove_cv; template struct __is_void_helper : public false_type { }; _DEFINE_SPEC(0, __is_void_helper, void, true) // primary type categories. /// is_void template struct is_void : public integral_constant::type>::value)> { }; template struct __is_integral_helper : public false_type { }; _DEFINE_SPEC(0, __is_integral_helper, bool, true) _DEFINE_SPEC(0, __is_integral_helper, char, true) _DEFINE_SPEC(0, __is_integral_helper, signed char, true) _DEFINE_SPEC(0, __is_integral_helper, unsigned char, true) #ifdef _GLIBCXX_USE_WCHAR_T _DEFINE_SPEC(0, __is_integral_helper, wchar_t, true) #endif _DEFINE_SPEC(0, __is_integral_helper, char16_t, true) _DEFINE_SPEC(0, __is_integral_helper, char32_t, true) _DEFINE_SPEC(0, __is_integral_helper, short, true) _DEFINE_SPEC(0, __is_integral_helper, unsigned short, true) _DEFINE_SPEC(0, __is_integral_helper, int, true) _DEFINE_SPEC(0, __is_integral_helper, unsigned int, true) _DEFINE_SPEC(0, __is_integral_helper, long, true) _DEFINE_SPEC(0, __is_integral_helper, unsigned long, true) _DEFINE_SPEC(0, __is_integral_helper, long long, true) _DEFINE_SPEC(0, __is_integral_helper, unsigned long long, true) /// is_integral template struct is_integral : public integral_constant::type>::value)> { }; template struct __is_floating_point_helper : public false_type { }; _DEFINE_SPEC(0, __is_floating_point_helper, float, true) _DEFINE_SPEC(0, __is_floating_point_helper, double, true) _DEFINE_SPEC(0, __is_floating_point_helper, long double, true) /// is_floating_point template struct is_floating_point : public integral_constant::type>::value)> { }; /// is_array template struct is_array : public false_type { }; template struct is_array<_Tp[_Size]> : public true_type { }; template struct is_array<_Tp[]> : public true_type { }; template struct __is_pointer_helper : public false_type { }; _DEFINE_SPEC(1, __is_pointer_helper, _Tp*, true) /// is_pointer template struct is_pointer : public integral_constant::type>::value)> { }; /// is_reference template struct is_reference; /// is_function template struct is_function; template struct __is_member_object_pointer_helper : public false_type { }; _DEFINE_SPEC(2, __is_member_object_pointer_helper, _Tp _Cp::*, !is_function<_Tp>::value) /// is_member_object_pointer template struct is_member_object_pointer : public integral_constant::type>::value)> { }; template struct __is_member_function_pointer_helper : public false_type { }; _DEFINE_SPEC(2, __is_member_function_pointer_helper, _Tp _Cp::*, is_function<_Tp>::value) /// is_member_function_pointer template struct is_member_function_pointer : public integral_constant::type>::value)> { }; /// is_enum template struct is_enum : public integral_constant { }; /// is_union template struct is_union : public integral_constant { }; /// is_class template struct is_class : public integral_constant { }; /// is_function template struct is_function : public false_type { }; template struct is_function<_Res(_ArgTypes...)> : public true_type { }; template struct is_function<_Res(_ArgTypes......)> : public true_type { }; template struct is_function<_Res(_ArgTypes...) const> : public true_type { }; template struct is_function<_Res(_ArgTypes......) const> : public true_type { }; template struct is_function<_Res(_ArgTypes...) volatile> : public true_type { }; template struct is_function<_Res(_ArgTypes......) volatile> : public true_type { }; template struct is_function<_Res(_ArgTypes...) const volatile> : public true_type { }; template struct is_function<_Res(_ArgTypes......) const volatile> : public true_type { }; template struct __is_nullptr_t_helper : public false_type { }; _DEFINE_SPEC(0, __is_nullptr_t_helper, std::nullptr_t, true) // __is_nullptr_t (extension). template struct __is_nullptr_t : public integral_constant::type>::value)> { }; // composite type traits. /// is_arithmetic template struct is_arithmetic : public integral_constant::value || is_floating_point<_Tp>::value)> { }; /// is_fundamental template struct is_fundamental : public integral_constant::value || is_void<_Tp>::value)> { }; /// is_object template struct is_object : public integral_constant::value || is_reference<_Tp>::value || is_void<_Tp>::value)> { }; /// is_member_pointer template struct is_member_pointer; /// is_scalar template struct is_scalar : public integral_constant::value || is_enum<_Tp>::value || is_pointer<_Tp>::value || is_member_pointer<_Tp>::value || __is_nullptr_t<_Tp>::value)> { }; /// is_compound template struct is_compound : public integral_constant::value> { }; /// is_member_pointer template struct __is_member_pointer_helper : public false_type { }; _DEFINE_SPEC(2, __is_member_pointer_helper, _Tp _Cp::*, true) template struct is_member_pointer : public integral_constant::type>::value)> { }; // type properties. /// is_const template struct is_const : public false_type { }; template struct is_const<_Tp const> : public true_type { }; /// is_volatile template struct is_volatile : public false_type { }; template struct is_volatile<_Tp volatile> : public true_type { }; /// is_empty template struct is_empty : public integral_constant { }; /// is_polymorphic template struct is_polymorphic : public integral_constant { }; /// is_abstract template struct is_abstract : public integral_constant { }; /// has_virtual_destructor template struct has_virtual_destructor : public integral_constant { }; /// alignment_of template struct alignment_of : public integral_constant { }; /// rank template struct rank : public integral_constant { }; template struct rank<_Tp[_Size]> : public integral_constant::value> { }; template struct rank<_Tp[]> : public integral_constant::value> { }; /// extent template struct extent : public integral_constant { }; template struct extent<_Tp[_Size], _Uint> : public integral_constant::value> { }; template struct extent<_Tp[], _Uint> : public integral_constant::value> { }; // relationships between types [4.6]. /// is_same template struct is_same : public false_type { }; template struct is_same<_Tp, _Tp> : public true_type { }; // const-volatile modifications [4.7.1]. /// remove_const template struct remove_const { typedef _Tp type; }; template struct remove_const<_Tp const> { typedef _Tp type; }; /// remove_volatile template struct remove_volatile { typedef _Tp type; }; template struct remove_volatile<_Tp volatile> { typedef _Tp type; }; /// remove_cv template struct remove_cv { typedef typename remove_const::type>::type type; }; /// add_const template struct add_const { typedef _Tp const type; }; /// add_volatile template struct add_volatile { typedef _Tp volatile type; }; /// add_cv template struct add_cv { typedef typename add_const::type>::type type; }; // array modifications. /// remove_extent template struct remove_extent { typedef _Tp type; }; template struct remove_extent<_Tp[_Size]> { typedef _Tp type; }; template struct remove_extent<_Tp[]> { typedef _Tp type; }; /// remove_all_extents template struct remove_all_extents { typedef _Tp type; }; template struct remove_all_extents<_Tp[_Size]> { typedef typename remove_all_extents<_Tp>::type type; }; template struct remove_all_extents<_Tp[]> { typedef typename remove_all_extents<_Tp>::type type; }; // pointer modifications. template struct __remove_pointer_helper { typedef _Tp type; }; template struct __remove_pointer_helper<_Tp, _Up*> { typedef _Up type; }; /// remove_pointer template struct remove_pointer : public __remove_pointer_helper<_Tp, typename remove_cv<_Tp>::type> { }; template struct remove_reference; /// add_pointer template struct add_pointer { typedef typename remove_reference<_Tp>::type* type; }; // Primary classification traits. /// is_lvalue_reference template struct is_lvalue_reference : public false_type { }; template struct is_lvalue_reference<_Tp&> : public true_type { }; /// is_rvalue_reference template struct is_rvalue_reference : public false_type { }; template struct is_rvalue_reference<_Tp&&> : public true_type { }; // Secondary classification traits. /// is_reference template struct is_reference : public integral_constant::value || is_rvalue_reference<_Tp>::value)> { }; // Reference transformations. /// remove_reference template struct remove_reference { typedef _Tp type; }; template struct remove_reference<_Tp&> { typedef _Tp type; }; template struct remove_reference<_Tp&&> { typedef _Tp type; }; template::value && !is_void<_Tp>::value, bool = is_rvalue_reference<_Tp>::value> struct __add_lvalue_reference_helper { typedef _Tp type; }; template struct __add_lvalue_reference_helper<_Tp, true, false> { typedef _Tp& type; }; template struct __add_lvalue_reference_helper<_Tp, false, true> { typedef typename remove_reference<_Tp>::type& type; }; /// add_lvalue_reference template struct add_lvalue_reference : public __add_lvalue_reference_helper<_Tp> { }; template::value && !is_void<_Tp>::value> struct __add_rvalue_reference_helper { typedef _Tp type; }; template struct __add_rvalue_reference_helper<_Tp, true> { typedef _Tp&& type; }; /// add_rvalue_reference template struct add_rvalue_reference : public __add_rvalue_reference_helper<_Tp> { }; // Scalar properties and transformations. template::value, bool = is_floating_point<_Tp>::value> struct __is_signed_helper : public false_type { }; template struct __is_signed_helper<_Tp, false, true> : public true_type { }; template struct __is_signed_helper<_Tp, true, false> : public integral_constant(_Tp(-1) < _Tp(0))> { }; /// is_signed template struct is_signed : public integral_constant::value> { }; /// is_unsigned template struct is_unsigned : public integral_constant::value && !is_signed<_Tp>::value)> { }; // Member introspection. /// is_trivial template struct is_trivial : public integral_constant { }; /// is_standard_layout template struct is_standard_layout : public integral_constant { }; /// is_pod // Could use is_standard_layout && is_trivial instead of the builtin. template struct is_pod : public integral_constant { }; /// is_literal_type template struct is_literal_type : public integral_constant { }; template typename add_rvalue_reference<_Tp>::type declval() noexcept; template class __is_constructible_helper : public __sfinae_types { template static decltype(_Tp1(declval<_Args1>()...), __one()) __test(int); template static __two __test(...); public: static const bool __value = sizeof(__test<_Tp, _Args...>(0)) == 1; }; template class __is_constructible_helper<_Tp, _Arg> : public __sfinae_types { template static decltype(static_cast<_Tp1>(declval<_Arg1>()), __one()) __test(int); template static __two __test(...); public: static const bool __value = sizeof(__test<_Tp, _Arg>(0)) == 1; }; /// is_constructible // XXX FIXME // The C++0x specifications require front-end support, see N2255. template struct is_constructible : public integral_constant::__value> { }; template struct __is_nt_constructible_helper { static const bool __value = false; }; template struct __is_nt_constructible_helper { static const bool __value = noexcept(_Tp(declval<_Args>()...)); }; template struct __is_nt_constructible_helper { static const bool __value = noexcept(static_cast<_Tp>(declval<_Arg>())); }; /// is_nothrow_constructible template struct is_nothrow_constructible : public integral_constant::value, _Tp, _Args...>::__value> { }; /// has_trivial_default_constructor template struct has_trivial_default_constructor : public integral_constant { }; /// has_trivial_copy_constructor template struct has_trivial_copy_constructor : public integral_constant { }; /// has_trivial_copy_assign template struct has_trivial_copy_assign : public integral_constant { }; /// has_trivial_destructor template struct has_trivial_destructor : public integral_constant { }; /// has_nothrow_default_constructor template struct has_nothrow_default_constructor : public integral_constant { }; /// has_nothrow_copy_constructor template struct has_nothrow_copy_constructor : public integral_constant { }; /// has_nothrow_copy_assign template struct has_nothrow_copy_assign : public integral_constant { }; // Relationships between types. /// is_base_of template struct is_base_of : public integral_constant { }; template::value || is_function<_To>::value || is_array<_To>::value)> struct __is_convertible_helper { static const bool __value = is_void<_To>::value; }; template class __is_convertible_helper<_From, _To, false> : public __sfinae_types { template static void __test_aux(_To1); template static decltype(__test_aux<_To1>(std::declval<_From1>()), __one()) __test(int); template static __two __test(...); public: static const bool __value = sizeof(__test<_From, _To>(0)) == 1; }; /// is_convertible // XXX FIXME // The C++0x specifications require front-end support, see N2255. template struct is_convertible : public integral_constant::__value> { }; /// is_explicitly_convertible template struct is_explicitly_convertible : public is_constructible<_To, _From> { }; template struct __aligned_storage_msa { union __type { unsigned char __data[_Len]; struct __attribute__((__aligned__)) { } __align; }; }; /** * @brief Alignment type. * * The value of _Align is a default-alignment which shall be the * most stringent alignment requirement for any C++ object type * whose size is no greater than _Len (3.9). The member typedef * type shall be a POD type suitable for use as uninitialized * storage for any object whose size is at most _Len and whose * alignment is a divisor of _Align. */ template::__type)> struct aligned_storage { union type { unsigned char __data[_Len]; struct __attribute__((__aligned__((_Align)))) { } __align; }; }; // Define a nested type if some predicate holds. // Primary template. /// enable_if template struct enable_if { }; // Partial specialization for true. template struct enable_if { typedef _Tp type; }; // A conditional expression, but for types. If true, first, if false, second. // Primary template. /// conditional template struct conditional { typedef _Iftrue type; }; // Partial specialization for false. template struct conditional { typedef _Iffalse type; }; // Decay trait for arrays and functions, used for perfect forwarding // in make_pair, make_tuple, etc. template::value, bool _IsFunction = is_function<_Up>::value> struct __decay_selector; // NB: DR 705. template struct __decay_selector<_Up, false, false> { typedef typename remove_cv<_Up>::type __type; }; template struct __decay_selector<_Up, true, false> { typedef typename remove_extent<_Up>::type* __type; }; template struct __decay_selector<_Up, false, true> { typedef typename add_pointer<_Up>::type __type; }; /// decay template class decay { typedef typename remove_reference<_Tp>::type __remove_type; public: typedef typename __decay_selector<__remove_type>::__type type; }; template class reference_wrapper; // Helper which adds a reference to a type when given a reference_wrapper template struct __strip_reference_wrapper { typedef _Tp __type; }; template struct __strip_reference_wrapper > { typedef _Tp& __type; }; template struct __strip_reference_wrapper > { typedef _Tp& __type; }; template struct __decay_and_strip { typedef typename __strip_reference_wrapper< typename decay<_Tp>::type>::__type __type; }; // Utility for constructing identically cv-qualified types. template struct __cv_selector; template struct __cv_selector<_Unqualified, false, false> { typedef _Unqualified __type; }; template struct __cv_selector<_Unqualified, false, true> { typedef volatile _Unqualified __type; }; template struct __cv_selector<_Unqualified, true, false> { typedef const _Unqualified __type; }; template struct __cv_selector<_Unqualified, true, true> { typedef const volatile _Unqualified __type; }; template::value, bool _IsVol = is_volatile<_Qualified>::value> class __match_cv_qualifiers { typedef __cv_selector<_Unqualified, _IsConst, _IsVol> __match; public: typedef typename __match::__type __type; }; // Utility for finding the unsigned versions of signed integral types. template struct __make_unsigned { typedef _Tp __type; }; template<> struct __make_unsigned { typedef unsigned char __type; }; template<> struct __make_unsigned { typedef unsigned char __type; }; template<> struct __make_unsigned { typedef unsigned short __type; }; template<> struct __make_unsigned { typedef unsigned int __type; }; template<> struct __make_unsigned { typedef unsigned long __type; }; template<> struct __make_unsigned { typedef unsigned long long __type; }; // Select between integral and enum: not possible to be both. template::value, bool _IsEnum = is_enum<_Tp>::value> class __make_unsigned_selector; template class __make_unsigned_selector<_Tp, true, false> { typedef __make_unsigned::type> __unsignedt; typedef typename __unsignedt::__type __unsigned_type; typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned; public: typedef typename __cv_unsigned::__type __type; }; template class __make_unsigned_selector<_Tp, false, true> { // With -fshort-enums, an enum may be as small as a char. typedef unsigned char __smallest; static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest); static const bool __b1 = sizeof(_Tp) <= sizeof(unsigned short); static const bool __b2 = sizeof(_Tp) <= sizeof(unsigned int); typedef conditional<__b2, unsigned int, unsigned long> __cond2; typedef typename __cond2::type __cond2_type; typedef conditional<__b1, unsigned short, __cond2_type> __cond1; typedef typename __cond1::type __cond1_type; public: typedef typename conditional<__b0, __smallest, __cond1_type>::type __type; }; // Given an integral/enum type, return the corresponding unsigned // integer type. // Primary template. /// make_unsigned template struct make_unsigned { typedef typename __make_unsigned_selector<_Tp>::__type type; }; // Integral, but don't define. template<> struct make_unsigned; // Utility for finding the signed versions of unsigned integral types. template struct __make_signed { typedef _Tp __type; }; template<> struct __make_signed { typedef signed char __type; }; template<> struct __make_signed { typedef signed char __type; }; template<> struct __make_signed { typedef signed short __type; }; template<> struct __make_signed { typedef signed int __type; }; template<> struct __make_signed { typedef signed long __type; }; template<> struct __make_signed { typedef signed long long __type; }; // Select between integral and enum: not possible to be both. template::value, bool _IsEnum = is_enum<_Tp>::value> class __make_signed_selector; template class __make_signed_selector<_Tp, true, false> { typedef __make_signed::type> __signedt; typedef typename __signedt::__type __signed_type; typedef __match_cv_qualifiers<_Tp, __signed_type> __cv_signed; public: typedef typename __cv_signed::__type __type; }; template class __make_signed_selector<_Tp, false, true> { // With -fshort-enums, an enum may be as small as a char. typedef signed char __smallest; static const bool __b0 = sizeof(_Tp) <= sizeof(__smallest); static const bool __b1 = sizeof(_Tp) <= sizeof(signed short); static const bool __b2 = sizeof(_Tp) <= sizeof(signed int); typedef conditional<__b2, signed int, signed long> __cond2; typedef typename __cond2::type __cond2_type; typedef conditional<__b1, signed short, __cond2_type> __cond1; typedef typename __cond1::type __cond1_type; public: typedef typename conditional<__b0, __smallest, __cond1_type>::type __type; }; // Given an integral/enum type, return the corresponding signed // integer type. // Primary template. /// make_signed template struct make_signed { typedef typename __make_signed_selector<_Tp>::__type type; }; // Integral, but don't define. template<> struct make_signed; /// common_type template struct common_type; template struct common_type<_Tp> { typedef _Tp type; }; template struct common_type<_Tp, _Up> { typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; }; template struct common_type<_Tp, _Up, _Vp...> { typedef typename common_type::type, _Vp...>::type type; }; /// declval template struct __declval_protector { static const bool __stop = false; static typename add_rvalue_reference<_Tp>::type __delegate(); }; template inline typename add_rvalue_reference<_Tp>::type declval() noexcept { static_assert(__declval_protector<_Tp>::__stop, "declval() must not be used!"); return __declval_protector<_Tp>::__delegate(); } /// result_of template class result_of; template struct _Result_of_memobj; template struct _Result_of_memobj<_Res _Class::*, _Arg> { private: typedef _Res _Class::* _Func; template static _Tp _S_get(const _Class&); template static decltype(*std::declval<_Tp>()) _S_get(...); public: typedef decltype(_S_get<_Arg>(std::declval<_Arg>()).*std::declval<_Func>()) __type; }; template struct _Result_of_memfun; template struct _Result_of_memfun<_Res _Class::*, _Arg, _Args...> { private: typedef _Res _Class::* _Func; template static _Tp _S_get(const _Class&); template static decltype(*std::declval<_Tp>()) _S_get(...); public: typedef decltype((_S_get<_Arg>(std::declval<_Arg>()).*std::declval<_Func>()) (std::declval<_Args>()...) ) __type; }; template struct _Result_of_impl; template struct _Result_of_impl { typedef decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) ) __type; }; template struct _Result_of_impl : _Result_of_memobj::type, _Arg> { typedef typename _Result_of_memobj< typename remove_reference<_MemPtr>::type, _Arg>::__type __type; }; template struct _Result_of_impl : _Result_of_memfun::type, _Arg, _ArgTypes...> { typedef typename _Result_of_memfun< typename remove_reference<_MemPtr>::type, _Arg, _ArgTypes...>::__type __type; }; template struct result_of<_Functor(_ArgTypes...)> : _Result_of_impl::type >::value, is_member_function_pointer< typename remove_reference<_Functor>::type >::value, _Functor, _ArgTypes...> { typedef typename _Result_of_impl< is_member_object_pointer< typename remove_reference<_Functor>::type >::value, is_member_function_pointer< typename remove_reference<_Functor>::type >::value, _Functor, _ArgTypes...>::__type type; }; /** * Use SFINAE to determine if the type _Tp has a publicly-accessible * member type _NTYPE. */ #define _GLIBCXX_HAS_NESTED_TYPE(_NTYPE) \ template \ class __has_##_NTYPE##_helper \ : __sfinae_types \ { \ template \ struct _Wrap_type \ { }; \ \ template \ static __one __test(_Wrap_type*); \ \ template \ static __two __test(...); \ \ public: \ static const bool value = sizeof(__test<_Tp>(0)) == 1; \ }; \ \ template \ struct __has_##_NTYPE \ : integral_constant::type>::value> \ { }; #undef _DEFINE_SPEC_0_HELPER #undef _DEFINE_SPEC_1_HELPER #undef _DEFINE_SPEC_2_HELPER #undef _DEFINE_SPEC // @} group metaprogramming _GLIBCXX_END_NAMESPACE_VERSION } // namespace #endif // __GXX_EXPERIMENTAL_CXX0X__ #endif // _GLIBCXX_TYPE_TRAITS