diff options
Diffstat (limited to 'libstdc++-v3/include/tr1/type_traits')
-rw-r--r-- | libstdc++-v3/include/tr1/type_traits | 429 |
1 files changed, 160 insertions, 269 deletions
diff --git a/libstdc++-v3/include/tr1/type_traits b/libstdc++-v3/include/tr1/type_traits index 12609dd41b4..55f585f9f4b 100644 --- a/libstdc++-v3/include/tr1/type_traits +++ b/libstdc++-v3/include/tr1/type_traits @@ -18,6 +18,15 @@ // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, // USA. +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + /** @file * This is a TR1 C++ Library header. */ @@ -33,7 +42,7 @@ namespace std { namespace tr1 { - // For use in is_enum, is_abstract and elsewhere. + // For use in __conv_helper, is_abstract and elsewhere. struct __sfinae_types { typedef char __one; @@ -151,262 +160,31 @@ namespace tr1 _DEFINE_SPEC(2, is_member_object_pointer, _Tp _Cp::*, !is_function<_Tp>::value) - // Due to c++/19076, for the time being we cannot use the correct, neat - // implementation :-( - // - // template<typename> - // struct is_member_function_pointer - // : public false_type { }; - // _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*, - // is_function<_Tp>::value) - // - // Temporary workaround for member functions with up to 15 arguments: template<typename> - struct __is_mfp_helper - { static const bool __value = false; }; - - template<typename _Rt, typename _Cp> - struct __is_mfp_helper<_Rt (_Cp::*) ()> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp> - struct __is_mfp_helper<_Rt (_Cp::*) (...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12, typename _A13> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12, - _A13)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12, typename _A13> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12, - _A13, ...)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12, typename _A13, typename _A14> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12, - _A13, _A14)> - { static const bool __value = true; }; - - template<typename _Rt, typename _Cp, typename _A0, typename _A1, typename _A2, - typename _A3, typename _A4, typename _A5, typename _A6, typename _A7, - typename _A8, typename _A9, typename _A10, typename _A11, - typename _A12, typename _A13, typename _A14> - struct __is_mfp_helper<_Rt (_Cp::*) (_A0, _A1, _A2, _A3, _A4, _A5, _A6, - _A7, _A8, _A9, _A10, _A11, _A12, - _A13, _A14, ...)> - { static const bool __value = true; }; + struct is_member_function_pointer + : public false_type { }; + _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*, + is_function<_Tp>::value) template<typename _Tp> - struct is_member_function_pointer - : public integral_constant<bool, (__is_mfp_helper<typename - remove_cv<_Tp>::type>::__value)> + struct is_enum + : public integral_constant<bool, !(is_fundamental<_Tp>::value + || is_array<_Tp>::value + || is_pointer<_Tp>::value + || is_reference<_Tp>::value + || is_member_pointer<_Tp>::value + || is_function<_Tp>::value + || __is_union_or_class<_Tp>::value)> { }; - template<typename _Tp, bool = (is_fundamental<_Tp>::value - || is_array<_Tp>::value - || is_pointer<_Tp>::value - || is_reference<_Tp>::value - || is_member_pointer<_Tp>::value - || is_function<_Tp>::value)> - struct __is_enum_helper - : public __sfinae_types - { - private: - static __one __test(bool); - static __one __test(char); - static __one __test(signed char); - static __one __test(unsigned char); -#ifdef _GLIBCXX_USE_WCHAR_T - static __one __test(wchar_t); -#endif - static __one __test(short); - static __one __test(unsigned short); - static __one __test(int); - static __one __test(unsigned int); - static __one __test(long); - static __one __test(unsigned long); - static __one __test(long long); - static __one __test(unsigned long long); - static __two __test(...); - - struct __convert - { operator _Tp() const; }; - - public: - static const bool __value = sizeof(__test(__convert())) == 1; - }; - - template<typename _Tp> - struct __is_enum_helper<_Tp, true> - { static const bool __value = false; }; + template<typename> + struct is_union { }; - template<typename _Tp> - struct is_enum - : public integral_constant<bool, __is_enum_helper<_Tp>::__value> { }; + template<typename> + struct is_class { }; - template<typename _Tp, bool = is_void<_Tp>::value> + template<typename _Tp, bool = (is_void<_Tp>::value + || is_reference<_Tp>::value)> struct __is_function_helper { static const bool __value = (__conv_helper<typename @@ -461,6 +239,26 @@ namespace tr1 (is_member_object_pointer<_Tp>::value || is_member_function_pointer<_Tp>::value)> { }; + + template<typename _Tp> + struct __is_union_or_class_helper + : public __sfinae_types + { + private: + template<typename _Up> + static __one __test(int _Up::*); + template<typename> + static __two __test(...); + + public: + static const bool __value = sizeof(__test<_Tp>(0)) == 1; + }; + + // Extension. + template<typename _Tp> + struct __is_union_or_class + : public integral_constant<bool, __is_union_or_class_helper<_Tp>::__value> + { }; /// @brief type properties [4.5.3]. template<typename> @@ -486,26 +284,21 @@ namespace tr1 remove_all_extents<_Tp>::type>::value)> { }; - template<typename> - struct __is_empty_helper_1 - { }; - - template<typename _Tp> - struct __is_empty_helper_2 - : public _Tp { }; - - // Unfortunately, without compiler support we cannot tell union from - // class types, and is_empty doesn't work at all with the former. - template<typename _Tp, bool = (is_fundamental<_Tp>::value - || is_array<_Tp>::value - || is_pointer<_Tp>::value - || is_reference<_Tp>::value - || is_member_pointer<_Tp>::value - || is_enum<_Tp>::value - || is_function<_Tp>::value)> + // N.B. Without compiler support we cannot tell union from class types, + // and is_empty and is_polymorphic don't work at all with the former. + template<typename _Tp, bool = !__is_union_or_class<_Tp>::value> struct __is_empty_helper - { static const bool __value = (sizeof(__is_empty_helper_1<_Tp>) - == sizeof(__is_empty_helper_2<_Tp>)); }; + { + private: + template<typename> + struct __first { }; + template<typename _Up> + struct __second + : public _Up { }; + + public: + static const bool __value = sizeof(__first<_Tp>) == sizeof(__second<_Tp>); + }; template<typename _Tp> struct __is_empty_helper<_Tp, true> @@ -516,6 +309,34 @@ namespace tr1 : public integral_constant<bool, __is_empty_helper<_Tp>::__value> { }; + template<typename _Tp, bool = !__is_union_or_class<_Tp>::value> + struct __is_polymorphic_helper + { + private: + template<typename _Up> + struct __first + : public _Up { }; + template<typename _Up> + struct __second + : public _Up + { + virtual void __dummy(); + virtual ~__second(); + }; + + public: + static const bool __value = sizeof(__first<_Tp>) == sizeof(__second<_Tp>); + }; + + template<typename _Tp> + struct __is_polymorphic_helper<_Tp, true> + { static const bool __value = false; }; + + template<typename _Tp> + struct is_polymorphic + : public integral_constant<bool, __is_polymorphic_helper<_Tp>::__value> + { }; + // Exploit the resolution DR core/337. template<typename _Tp, bool = !is_object<_Tp>::value> struct __is_abstract_helper @@ -638,6 +459,76 @@ namespace tr1 struct is_same<_Tp, _Tp> : public true_type { }; + // See Daveed Vandevoorde explanation in http://tinyurl.com/502f. + // Also see Rani Sharoni in http://tinyurl.com/6jvyq. + template<typename _Base, typename _Derived, + bool = (!__is_union_or_class<_Base>::value + || !__is_union_or_class<_Derived>::value + || is_same<_Base, _Derived>::value)> + struct __is_base_of_helper + : public __sfinae_types + { + private: + typedef typename remove_cv<_Base>::type _NoCv_Base; + typedef typename remove_cv<_Derived>::type _NoCv_Derived; + + template<typename _Up> + static __one __test(_NoCv_Derived&, _Up); + static __two __test(_NoCv_Base&, int); + + struct _Conv + { + operator _NoCv_Derived&(); + operator _NoCv_Base&() const; + }; + + public: + static const bool __value = sizeof(__test(_Conv(), 0)) == 1; + }; + + template<typename _Base, typename _Derived> + struct __is_base_of_helper<_Base, _Derived, true> + { static const bool __value = is_same<_Base, _Derived>::value; }; + + template<typename _Base, typename _Derived> + struct is_base_of + : public integral_constant<bool, + __is_base_of_helper<_Base, _Derived>::__value> + { }; + + template<typename _Tp> + struct __is_int_or_cref + { + typedef typename remove_reference<_Tp>::type __rr_Tp; + static const bool __value = (is_integral<_Tp>::value + || (is_integral<__rr_Tp>::value + && is_const<__rr_Tp>::value + && !is_volatile<__rr_Tp>::value)); + }; + + template<typename _From, typename _To, + bool = (is_function<_To>::value || is_array<_To>::value + // This special case is here only to avoid warnings. + || (is_floating_point<typename + remove_reference<_From>::type>::value + && __is_int_or_cref<_To>::__value))> + struct __is_convertible_helper + { + // "An imaginary lvalue of type From...". + static const bool __value = (__conv_helper<typename + add_reference<_From>::type, _To>::__value); + }; + + template<typename _From, typename _To> + struct __is_convertible_helper<_From, _To, true> + { static const bool __value = __is_int_or_cref<_To>::__value; }; + + template<typename _From, typename _To> + struct is_convertible + : public integral_constant<bool, + __is_convertible_helper<_From, _To>::__value> + { }; + /// @brief const-volatile modifications [4.7.1]. template<typename _Tp> struct remove_const |