C++11 元编程学习
2022/1/15 17:33:58
本文主要是介绍C++11 元编程学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
主要对C++11中的<type_traist>中的代码进行简单的注释说明
// C++11 <type_traits> -*- C++ -*- // Copyright (C) 2007-2015 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 // <http://www.gnu.org/licenses/>. /** @file include/type_traits * This is a Standard C++ Library header. */ #ifndef _GLIBCXX_TYPE_TRAITS #define _GLIBCXX_TYPE_TRAITS 1 #pragma GCC system_header #if __cplusplus < 201103L # include <bits/c++0x_warning.h> #else #include <bits/c++config.h> #ifdef _GLIBCXX_USE_C99_STDINT_TR1 # if defined (__UINT_LEAST16_TYPE__) && defined(__UINT_LEAST32_TYPE__) namespace std { typedef __UINT_LEAST16_TYPE__ uint_least16_t; typedef __UINT_LEAST32_TYPE__ uint_least32_t; } # else # include <cstdint> # endif #endif namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @defgroup metaprogramming Metaprogramming * @ingroup utilities * * Template utilities for compile-time introspection and modification, * including type classification traits, type property inspection traits * and type transformation traits. * * @{ */ /// integral_constant: 常整数 template<typename _Tp, _Tp __v> struct integral_constant { static constexpr _Tp value = __v; typedef _Tp value_type; typedef integral_constant<_Tp, __v> type; constexpr operator value_type() const { return value; } #if __cplusplus > 201103L #define __cpp_lib_integral_constant_callable 201304 constexpr value_type operator()() const { return value; } #endif }; // 定义静态变量 template<typename _Tp, _Tp __v> constexpr _Tp integral_constant<_Tp, __v>::value; // 编译时布尔类型取值为true的类型 /// The type used as a compile-time boolean with true value. typedef integral_constant<bool, true> true_type; // 编译时布尔类型取值为false的类型 /// The type used as a compile-time boolean with false value. typedef integral_constant<bool, false> false_type; // 布尔类型常量 template<bool __v> using __bool_constant = integral_constant<bool, __v>; // Meta programming helper types. // 第一个参数为true时 ,返回第二个的数值 // 第二个参数为false时,返回第三个的数值 template<bool, typename, typename> struct conditional; template<typename...> struct __or_; // __or_的无类型特化版本, __or_<>::value = false_type::value template<> struct __or_<> : public false_type { }; // __or_特化版本, __or_<_B1>::value = _B1::value template<typename _B1> struct __or_<_B1> : public _B1 { }; // __or_特化版本: __or_<_B1, _B2>::value = 如果_B1::value = true时,返回_B1::value; 如果_B2::value = true时, 返回_B2::value template<typename _B1, typename _B2> struct __or_<_B1, _B2> : public conditional<_B1::value, _B1, _B2>::type { }; // __or_三个类型及以上类型的特化版本: template<typename _B1, typename _B2, typename _B3, typename... _Bn> struct __or_<_B1, _B2, _B3, _Bn...> : public conditional<_B1::value, _B1, __or_<_B2, _B3, _Bn...>>::type { }; template<typename...> struct __and_; // __and_的无类型特化版本: __and_<>::value = true_type::value template<> struct __and_<> : public true_type { }; // 单类型特化版本: __and_<_B1>::value = _B1:value template<typename _B1> struct __and_<_B1> : public _B1 { }; // 两个类型特化版本: __and_<_B1, _B2>::value = _B1::value = true, 则返回 _B2::value; 如果_B1::value = false, 则返回 _B1::value // 即一个取值为false, 则不在进行判断 template<typename _B1, typename _B2> struct __and_<_B1, _B2> : public conditional<_B1::value, _B2, _B1>::type { }; template<typename _B1, typename _B2, typename _B3, typename... _Bn> struct __and_<_B1, _B2, _B3, _Bn...> : public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type { }; // 非 template<typename _Pp> struct __not_ : public integral_constant<bool, !_Pp::value> { }; // For several sfinae-friendly trait implementations we transport both the // result information (as the member type) and the failure information (no // member type). This is very similar to std::enable_if, but we cannot use // them, because we need to derive from them as an implementation detail. // 获取类型 template<typename _Tp> struct __success_type { typedef _Tp type; }; struct __failure_type { }; // Primary type categories. // 移除const和volatile的类型 template<typename> struct remove_cv; template<typename> struct __is_void_helper : public false_type { }; // 特化为void类型 template<> struct __is_void_helper<void> : public true_type { }; // 判断类型Tp是否为空类型 /// is_void template<typename _Tp> struct is_void : public __is_void_helper<typename remove_cv<_Tp>::type>::type { }; // 判断为整型: bool/char/signed char/unsigned char/wchar_t/char16_t/char32_t/ // short/unsigned short/int/unsigned int/long/unsigned long/long long/unsigned long long // __GLIBCXX_TYPE_INT_N_0/unsigned __GLIBCXX_TYPE_INT_N_0 // __GLIBCXX_TYPE_INT_N_1/unsigned __GLIBCXX_TYPE_INT_N_1 // __GLIBCXX_TYPE_INT_N_2/unsigned __GLIBCXX_TYPE_INT_N_2 // __GLIBCXX_TYPE_INT_N_3/unsigned __GLIBCXX_TYPE_INT_N_3 template<typename> struct __is_integral_helper : public false_type { }; template<> struct __is_integral_helper<bool> : public true_type { }; template<> struct __is_integral_helper<char> : public true_type { }; template<> struct __is_integral_helper<signed char> : public true_type { }; template<> struct __is_integral_helper<unsigned char> : public true_type { }; #ifdef _GLIBCXX_USE_WCHAR_T template<> struct __is_integral_helper<wchar_t> : public true_type { }; #endif template<> struct __is_integral_helper<char16_t> : public true_type { }; template<> struct __is_integral_helper<char32_t> : public true_type { }; template<> struct __is_integral_helper<short> : public true_type { }; template<> struct __is_integral_helper<unsigned short> : public true_type { }; template<> struct __is_integral_helper<int> : public true_type { }; template<> struct __is_integral_helper<unsigned int> : public true_type { }; template<> struct __is_integral_helper<long> : public true_type { }; template<> struct __is_integral_helper<unsigned long> : public true_type { }; template<> struct __is_integral_helper<long long> : public true_type { }; template<> struct __is_integral_helper<unsigned long long> : public true_type { }; // Conditionalizing on __STRICT_ANSI__ here will break any port that // uses one of these types for size_t. #if defined(__GLIBCXX_TYPE_INT_N_0) template<> struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_0> : public true_type { }; template<> struct __is_integral_helper<unsigned __GLIBCXX_TYPE_INT_N_0> : public true_type { }; #endif #if defined(__GLIBCXX_TYPE_INT_N_1) template<> struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_1> : public true_type { }; template<> struct __is_integral_helper<unsigned __GLIBCXX_TYPE_INT_N_1> : public true_type { }; #endif #if defined(__GLIBCXX_TYPE_INT_N_2) template<> struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_2> : public true_type { }; template<> struct __is_integral_helper<unsigned __GLIBCXX_TYPE_INT_N_2> : public true_type { }; #endif #if defined(__GLIBCXX_TYPE_INT_N_3) template<> struct __is_integral_helper<__GLIBCXX_TYPE_INT_N_3> : public true_type { }; template<> struct __is_integral_helper<unsigned __GLIBCXX_TYPE_INT_N_3> : public true_type { }; #endif // 判断_Tp是否为整型 /// is_integral template<typename _Tp> struct is_integral : public __is_integral_helper<typename remove_cv<_Tp>::type>::type { }; // 判断是否为浮点数类型: float/double/ long double / _GLIBCXX_USE_FLOAT128 template<typename> struct __is_floating_point_helper : public false_type { }; template<> struct __is_floating_point_helper<float> : public true_type { }; template<> struct __is_floating_point_helper<double> : public true_type { }; template<> struct __is_floating_point_helper<long double> : public true_type { }; #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128) template<> struct __is_floating_point_helper<__float128> : public true_type { }; #endif /// is_floating_point template<typename _Tp> struct is_floating_point : public __is_floating_point_helper<typename remove_cv<_Tp>::type>::type { }; // 判断是否为数组 /// is_array template<typename> struct is_array : public false_type { }; template<typename _Tp, std::size_t _Size> struct is_array<_Tp[_Size]> : public true_type { }; template<typename _Tp> struct is_array<_Tp[]> : public true_type { }; // 判断是否为指针 template<typename> struct __is_pointer_helper : public false_type { }; template<typename _Tp> struct __is_pointer_helper<_Tp*> : public true_type { }; /// is_pointer template<typename _Tp> struct is_pointer : public __is_pointer_helper<typename remove_cv<_Tp>::type>::type { }; // 判断是否为左值引用 /// is_lvalue_reference template<typename> struct is_lvalue_reference : public false_type { }; template<typename _Tp> struct is_lvalue_reference<_Tp&> : public true_type { }; // 判断是否为右值引用 /// is_rvalue_reference template<typename> struct is_rvalue_reference : public false_type { }; template<typename _Tp> struct is_rvalue_reference<_Tp&&> : public true_type { }; // 判断是否为函数 template<typename> struct is_function; // 判断是否为类的数据成员变量的指针 template<typename> struct __is_member_object_pointer_helper : public false_type { }; template<typename _Tp, typename _Cp> struct __is_member_object_pointer_helper<_Tp _Cp::*> : public integral_constant<bool, !is_function<_Tp>::value> { }; /// is_member_object_pointer template<typename _Tp> struct is_member_object_pointer : public __is_member_object_pointer_helper< typename remove_cv<_Tp>::type>::type { }; // 判断是否为类的成员函数的指针 template<typename> struct __is_member_function_pointer_helper : public false_type { }; template<typename _Tp, typename _Cp> struct __is_member_function_pointer_helper<_Tp _Cp::*> : public integral_constant<bool, is_function<_Tp>::value> { }; /// is_member_function_pointer template<typename _Tp> struct is_member_function_pointer : public __is_member_function_pointer_helper< typename remove_cv<_Tp>::type>::type { }; // 是否为枚举类型 /// is_enum template<typename _Tp> struct is_enum : public integral_constant<bool, __is_enum(_Tp)> { }; // 是否为union类型 /// is_union template<typename _Tp> struct is_union : public integral_constant<bool, __is_union(_Tp)> { }; // 是否为类 /// is_class template<typename _Tp> struct is_class : public integral_constant<bool, __is_class(_Tp)> { }; /// is_function // 默认返回 false template<typename> struct is_function : public false_type { }; // 判断函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...)> : public true_type { }; // 返回值为引用的函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) &> : public true_type { }; // 返回值为右值引用的函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) &&> : public true_type { }; // 变参数函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......)> : public true_type { }; // 返回值为引用的变参数函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) &> : public true_type { }; // 返回值为右值引用的变参数函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) &&> : public true_type { }; // const类型函数 template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const &&> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const &&> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) volatile> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) volatile &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) volatile &&> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) volatile> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) volatile &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) volatile &&> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const volatile> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const volatile &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...) const volatile &&> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const volatile> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const volatile &> : public true_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) const volatile &&> : public true_type { }; #define __cpp_lib_is_null_pointer 201309 // 判断是否为nullptr template<typename> struct __is_null_pointer_helper : public false_type { }; template<> struct __is_null_pointer_helper<std::nullptr_t> : public true_type { }; /// is_null_pointer (LWG 2247). template<typename _Tp> struct is_null_pointer : public __is_null_pointer_helper<typename remove_cv<_Tp>::type>::type { }; /// __is_nullptr_t (extension). template<typename _Tp> struct __is_nullptr_t : public is_null_pointer<_Tp> { }; // Composite type categories. // 判断是否为引用类型: 包含左值引用/右值引用 /// is_reference template<typename _Tp> struct is_reference : public __or_<is_lvalue_reference<_Tp>, is_rvalue_reference<_Tp>>::type { }; // 判断是否为算数的类型 /// is_arithmetic template<typename _Tp> struct is_arithmetic : public __or_<is_integral<_Tp>, is_floating_point<_Tp>>::type { }; // 是否为基础类型: 算数类型/void/nullptr /// is_fundamental template<typename _Tp> struct is_fundamental : public __or_<is_arithmetic<_Tp>, is_void<_Tp>, is_null_pointer<_Tp>>::type { }; // 是否为对象类型: 非(函数/引用/void)类型 /// is_object template<typename _Tp> struct is_object : public __not_<__or_<is_function<_Tp>, is_reference<_Tp>, is_void<_Tp>>>::type { }; // 判断是否为成员指针: 成员变量指针/成员函数指针 template<typename> struct is_member_pointer; // 判断是否为标量: 算数类型 | 枚举类型 | 指针类型 | 成员指针类型 | 空指针类型 /// is_scalar template<typename _Tp> struct is_scalar : public __or_<is_arithmetic<_Tp>, is_enum<_Tp>, is_pointer<_Tp>, is_member_pointer<_Tp>, is_null_pointer<_Tp>>::type { }; // 判断是否为复合类型: 即非基础类型 /// is_compound template<typename _Tp> struct is_compound : public integral_constant<bool, !is_fundamental<_Tp>::value> { }; template<typename _Tp> struct __is_member_pointer_helper : public false_type { }; template<typename _Tp, typename _Cp> struct __is_member_pointer_helper<_Tp _Cp::*> : public true_type { }; /// is_member_pointer template<typename _Tp> struct is_member_pointer : public __is_member_pointer_helper<typename remove_cv<_Tp>::type>::type { }; // Utility to detect referenceable types ([defns.referenceable]). // 判断是否可引用: 对象类型 | 引用类型 template<typename _Tp> struct __is_referenceable : public __or_<is_object<_Tp>, is_reference<_Tp>>::type { }; // 函数类型特化 template<typename _Res, typename... _Args> struct __is_referenceable<_Res(_Args...)> : public true_type { }; // 变参数函数类型特化 template<typename _Res, typename... _Args> struct __is_referenceable<_Res(_Args......)> : public true_type { }; // Type properties. // 判断是否为常量 /// is_const template<typename> struct is_const : public false_type { }; template<typename _Tp> struct is_const<_Tp const> : public true_type { }; // 判断是否为volatile /// is_volatile template<typename> struct is_volatile : public false_type { }; template<typename _Tp> struct is_volatile<_Tp volatile> : public true_type { }; // https://www.cnblogs.com/shuaihanhungry/p/5764019.html // 判断是否为"平凡的" /// is_trivial template<typename _Tp> struct is_trivial : public integral_constant<bool, __is_trivial(_Tp)> { }; // 是否为"平凡可拷贝的" // is_trivially_copyable template<typename _Tp> struct is_trivially_copyable : public integral_constant<bool, __is_trivially_copyable(_Tp)> { }; // 是否为standard 内存排布 /// is_standard_layout template<typename _Tp> struct is_standard_layout : public integral_constant<bool, __is_standard_layout(_Tp)> { }; // 是否为standard 内部排布 + 平凡类型(Plain Old Data, 即和C语言的struct一致,可以直接通过内存拷贝即可保持全部的成员变量保持不变) /// is_pod // Could use is_standard_layout && is_trivial instead of the builtin. template<typename _Tp> struct is_pod : public integral_constant<bool, __is_pod(_Tp)> { }; // 编译期计算量类型: 标量都符合 /// is_literal_type template<typename _Tp> struct is_literal_type : public integral_constant<bool, __is_literal_type(_Tp)> { }; // 是否为empty /// is_empty template<typename _Tp> struct is_empty : public integral_constant<bool, __is_empty(_Tp)> { }; // 是否为多态 /// is_polymorphic template<typename _Tp> struct is_polymorphic : public integral_constant<bool, __is_polymorphic(_Tp)> { }; // 是否为final: c++14及之后版本 #if __cplusplus >= 201402L #define __cpp_lib_is_final 201402L /// is_final template<typename _Tp> struct is_final : public integral_constant<bool, __is_final(_Tp)> { }; #endif // 是否为抽象类 /// is_abstract template<typename _Tp> struct is_abstract : public integral_constant<bool, __is_abstract(_Tp)> { }; // 是否为有符号类型: 首先必须是算数类型 template<typename _Tp, bool = is_arithmetic<_Tp>::value> struct __is_signed_helper : public false_type { }; template<typename _Tp> struct __is_signed_helper<_Tp, true> : public integral_constant<bool, _Tp(-1) < _Tp(0)> { }; /// is_signed template<typename _Tp> struct is_signed : public __is_signed_helper<_Tp>::type { }; // 判断是否为有符号类型 /// is_unsigned template<typename _Tp> struct is_unsigned : public __and_<is_arithmetic<_Tp>, __not_<is_signed<_Tp>>>::type { }; // Destructible and constructible type properties. // (1) 默认为 add_rvalue_reference<Tp>::value = Tp // (2) 如果为可引用的类型,则add_rvalue_reference<Tp>::value = Tp&& template<typename> struct add_rvalue_reference; /** * @brief Utility to simplify expressions used in unevaluated operands * @ingroup utilities */ template<typename _Tp> typename add_rvalue_reference<_Tp>::type declval() noexcept; template<typename, unsigned = 0> struct extent; template<typename> struct remove_all_extents; template<typename _Tp> struct __is_array_known_bounds : public integral_constant<bool, (extent<_Tp>::value > 0)> { }; template<typename _Tp> struct __is_array_unknown_bounds : public __and_<is_array<_Tp>, __not_<extent<_Tp>>>::type { }; // In N3290 is_destructible does not say anything about function // types and abstract types, see LWG 2049. This implementation // describes function types as non-destructible and all complete // object types as destructible, iff the explicit destructor // call expression is wellformed(合乎语法的). struct __do_is_destructible_impl { template<typename _Tp, typename = decltype(declval<_Tp&>().~_Tp())> static true_type __test(int); template<typename> static false_type __test(...); }; template<typename _Tp> struct __is_destructible_impl : public __do_is_destructible_impl { typedef decltype(__test<_Tp>(0)) type; }; template<typename _Tp, bool = __or_<is_void<_Tp>, __is_array_unknown_bounds<_Tp>, is_function<_Tp>>::value, bool = __or_<is_reference<_Tp>, is_scalar<_Tp>>::value> struct __is_destructible_safe; template<typename _Tp> struct __is_destructible_safe<_Tp, false, false> : public __is_destructible_impl<typename remove_all_extents<_Tp>::type>::type { }; template<typename _Tp> struct __is_destructible_safe<_Tp, true, false> : public false_type { }; template<typename _Tp> struct __is_destructible_safe<_Tp, false, true> : public true_type { }; // 可析构的: 不是void类型/数组/函数/引用/标量 /// is_destructible template<typename _Tp> struct is_destructible : public __is_destructible_safe<_Tp>::type { }; // is_nothrow_destructible requires that is_destructible is // satisfied as well. We realize that by mimicing the // implementation of is_destructible but refer to noexcept(expr) // instead of decltype(expr). struct __do_is_nt_destructible_impl { // 这里增加了析构函数为noexcept的判断 template<typename _Tp> static integral_constant<bool, noexcept(declval<_Tp&>().~_Tp())> __test(int); template<typename> static false_type __test(...); }; template<typename _Tp> struct __is_nt_destructible_impl : public __do_is_nt_destructible_impl { typedef decltype(__test<_Tp>(0)) type; }; template<typename _Tp, bool = __or_<is_void<_Tp>, __is_array_unknown_bounds<_Tp>, is_function<_Tp>>::value, bool = __or_<is_reference<_Tp>, is_scalar<_Tp>>::value> struct __is_nt_destructible_safe; template<typename _Tp> struct __is_nt_destructible_safe<_Tp, false, false> : public __is_nt_destructible_impl<typename remove_all_extents<_Tp>::type>::type { }; template<typename _Tp> struct __is_nt_destructible_safe<_Tp, true, false> : public false_type { }; template<typename _Tp> struct __is_nt_destructible_safe<_Tp, false, true> : public true_type { }; // 不抛出异常的析构,需要满足是可析构的 /// is_nothrow_destructible template<typename _Tp> struct is_nothrow_destructible : public __is_nt_destructible_safe<_Tp>::type { }; // 判断是否有默认构造函数 struct __do_is_default_constructible_impl { template<typename _Tp, typename = decltype(_Tp())> static true_type __test(int); template<typename> static false_type __test(...); }; template<typename _Tp> struct __is_default_constructible_impl : public __do_is_default_constructible_impl { typedef decltype(__test<_Tp>(0)) type; }; // 非void类型 & 包含默认构造函数 template<typename _Tp> struct __is_default_constructible_atom : public __and_<__not_<is_void<_Tp>>, __is_default_constructible_impl<_Tp>>::type { }; // 特化数组 template<typename _Tp, bool = is_array<_Tp>::value> struct __is_default_constructible_safe; // The following technique is a workaround for a current core language // restriction, which does not allow for array types to occur in // functional casts of the form T(). Complete arrays can be default- // constructed, if the element type is default-constructible, but // arrays with unknown bounds are not. // 特化已知size的数组 template<typename _Tp> struct __is_default_constructible_safe<_Tp, true> : public __and_<__is_array_known_bounds<_Tp>, __is_default_constructible_atom<typename remove_all_extents<_Tp>::type>>::type { }; template<typename _Tp> struct __is_default_constructible_safe<_Tp, false> : public __is_default_constructible_atom<_Tp>::type { }; // 判断是否有默认构造函数 /// is_default_constructible template<typename _Tp> struct is_default_constructible : public __is_default_constructible_safe<_Tp>::type { }; // Implementation of is_constructible. // The hardest part of this trait is the binary direct-initialization // case, because we hit into a functional cast of the form T(arg). // This implementation uses different strategies depending on the // target type to reduce the test overhead as much as possible: // // a) For a reference target type, we use a static_cast expression // modulo its extra cases. // // b) For a non-reference target type we use a ::new expression. struct __do_is_static_castable_impl { template<typename _From, typename _To, typename = decltype(static_cast<_To>(declval<_From>()))> static true_type __test(int); template<typename, typename> static false_type __test(...); }; template<typename _From, typename _To> struct __is_static_castable_impl : public __do_is_static_castable_impl { typedef decltype(__test<_From, _To>(0)) type; }; template<typename _From, typename _To> struct __is_static_castable_safe : public __is_static_castable_impl<_From, _To>::type { }; // 判断是否可以进行static_cast操作 // __is_static_castable template<typename _From, typename _To> struct __is_static_castable : public integral_constant<bool, (__is_static_castable_safe< _From, _To>::value)> { }; // Implementation for non-reference types. To meet the proper // variable definition semantics, we also need to test for // is_destructible in this case. // This form should be simplified by a single expression: // ::delete ::new _Tp(declval<_Arg>()), see c++/51222. struct __do_is_direct_constructible_impl { template<typename _Tp, typename _Arg, typename = decltype(::new _Tp(declval<_Arg>()))> static true_type __test(int); template<typename, typename> static false_type __test(...); }; template<typename _Tp, typename _Arg> struct __is_direct_constructible_impl : public __do_is_direct_constructible_impl { typedef decltype(__test<_Tp, _Arg>(0)) type; }; // 判断可以进行直接的new template<typename _Tp, typename _Arg> struct __is_direct_constructible_new_safe : public __and_<is_destructible<_Tp>, __is_direct_constructible_impl<_Tp, _Arg>>::type { }; // 判断是否为相同类型 template<typename, typename> struct is_same; // 判断是否为基类 template<typename, typename> struct is_base_of; // 移除引用 template<typename> struct remove_reference; // 非void 或 函数类型 template<typename _From, typename _To, bool = __not_<__or_<is_void<_From>, is_function<_From>>>::value> struct __is_base_to_derived_ref; // Detect whether we have a downcast situation during // reference binding. // 判断基类引用到子类引用: from和to的类型不相同 && from必须为to的基类 template<typename _From, typename _To> struct __is_base_to_derived_ref<_From, _To, true> { typedef typename remove_cv<typename remove_reference<_From >::type>::type __src_t; typedef typename remove_cv<typename remove_reference<_To >::type>::type __dst_t; typedef __and_<__not_<is_same<__src_t, __dst_t>>, is_base_of<__src_t, __dst_t>> type; static constexpr bool value = type::value; }; template<typename _From, typename _To> struct __is_base_to_derived_ref<_From, _To, false> : public false_type { }; // 判断左值引用 => 右值引用 template<typename _From, typename _To, bool = __and_<is_lvalue_reference<_From>, is_rvalue_reference<_To>>::value> struct __is_lvalue_to_rvalue_ref; // Detect whether we have an lvalue of non-function type // bound to a reference-compatible rvalue-reference. template<typename _From, typename _To> struct __is_lvalue_to_rvalue_ref<_From, _To, true> { typedef typename remove_cv<typename remove_reference< _From>::type>::type __src_t; typedef typename remove_cv<typename remove_reference< _To>::type>::type __dst_t; typedef __and_<__not_<is_function<__src_t>>, __or_<is_same<__src_t, __dst_t>, is_base_of<__dst_t, __src_t>>> type; static constexpr bool value = type::value; }; template<typename _From, typename _To> struct __is_lvalue_to_rvalue_ref<_From, _To, false> : public false_type { }; // Here we handle direct-initialization to a reference type as // equivalent to a static_cast modulo overshooting conversions. // These are restricted to the following conversions: // a) A base class value to a derived class reference // b) An lvalue to an rvalue-reference of reference-compatible // types that are not functions // 判断Arg是否可以转为Tp的引用类型 template<typename _Tp, typename _Arg> struct __is_direct_constructible_ref_cast : public __and_<__is_static_castable<_Arg, _Tp>, __not_<__or_<__is_base_to_derived_ref<_Arg, _Tp>, __is_lvalue_to_rvalue_ref<_Arg, _Tp> >>>::type { }; // 判断是否可以对new使用构造函数 template<typename _Tp, typename _Arg> struct __is_direct_constructible_new : public conditional<is_reference<_Tp>::value, __is_direct_constructible_ref_cast<_Tp, _Arg>, __is_direct_constructible_new_safe<_Tp, _Arg> >::type { }; // 判断是否可以直接调用构造函数 template<typename _Tp, typename _Arg> struct __is_direct_constructible : public __is_direct_constructible_new<_Tp, _Arg>::type { }; // Since default-construction and binary direct-initialization have // been handled separately, the implementation of the remaining // n-ary construction cases is rather straightforward. We can use // here a functional cast, because array types are excluded anyway // and this form is never interpreted as a C cast. // 判断是否可以调用非默认构造函数: Tp(args) struct __do_is_nary_constructible_impl { template<typename _Tp, typename... _Args, typename = decltype(_Tp(declval<_Args>()...))> static true_type __test(int); template<typename, typename...> static false_type __test(...); }; template<typename _Tp, typename... _Args> struct __is_nary_constructible_impl : public __do_is_nary_constructible_impl { typedef decltype(__test<_Tp, _Args...>(0)) type; }; template<typename _Tp, typename... _Args> struct __is_nary_constructible : public __is_nary_constructible_impl<_Tp, _Args...>::type { static_assert(sizeof...(_Args) > 1, "Only useful for > 1 arguments"); }; template<typename _Tp, typename... _Args> struct __is_constructible_impl : public __is_nary_constructible<_Tp, _Args...> { }; template<typename _Tp, typename _Arg> struct __is_constructible_impl<_Tp, _Arg> : public __is_direct_constructible<_Tp, _Arg> { }; template<typename _Tp> struct __is_constructible_impl<_Tp> : public is_default_constructible<_Tp> { }; // 判断构造函数 /// is_constructible template<typename _Tp, typename... _Args> struct is_constructible : public __is_constructible_impl<_Tp, _Args...>::type { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_copy_constructible_impl; template<typename _Tp> struct __is_copy_constructible_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_copy_constructible_impl<_Tp, true> : public is_constructible<_Tp, const _Tp&> { }; // 判断拷贝构造函数 /// is_copy_constructible template<typename _Tp> struct is_copy_constructible : public __is_copy_constructible_impl<_Tp> { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_move_constructible_impl; template<typename _Tp> struct __is_move_constructible_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_move_constructible_impl<_Tp, true> : public is_constructible<_Tp, _Tp&&> { }; // 判断移动拷贝构造函数 /// is_move_constructible template<typename _Tp> struct is_move_constructible : public __is_move_constructible_impl<_Tp> { }; // 判断非抛出异常默认构造函数 template<typename _Tp> struct __is_nt_default_constructible_atom : public integral_constant<bool, noexcept(_Tp())> { }; // 对数组特化 template<typename _Tp, bool = is_array<_Tp>::value> struct __is_nt_default_constructible_impl; // 对数组特化: 已知数组大小 template<typename _Tp> struct __is_nt_default_constructible_impl<_Tp, true> : public __and_<__is_array_known_bounds<_Tp>, __is_nt_default_constructible_atom<typename remove_all_extents<_Tp>::type>>::type { }; template<typename _Tp> struct __is_nt_default_constructible_impl<_Tp, false> : public __is_nt_default_constructible_atom<_Tp> { }; // 非抛出异常的默认构造函数 /// is_nothrow_default_constructible template<typename _Tp> struct is_nothrow_default_constructible : public __and_<is_default_constructible<_Tp>, __is_nt_default_constructible_impl<_Tp>>::type { }; // 非抛出异常的构造函数 template<typename _Tp, typename... _Args> struct __is_nt_constructible_impl : public integral_constant<bool, noexcept(_Tp(declval<_Args>()...))> { }; template<typename _Tp, typename _Arg> struct __is_nt_constructible_impl<_Tp, _Arg> : public integral_constant<bool, noexcept(static_cast<_Tp>(declval<_Arg>()))> { }; template<typename _Tp> struct __is_nt_constructible_impl<_Tp> : public is_nothrow_default_constructible<_Tp> { }; // 判断非抛出异常的构造函数 /// is_nothrow_constructible template<typename _Tp, typename... _Args> struct is_nothrow_constructible : public __and_<is_constructible<_Tp, _Args...>, __is_nt_constructible_impl<_Tp, _Args...>>::type { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_nothrow_copy_constructible_impl; template<typename _Tp> struct __is_nothrow_copy_constructible_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_nothrow_copy_constructible_impl<_Tp, true> : public is_nothrow_constructible<_Tp, const _Tp&> { }; // 非抛出异常的拷贝构造函数 /// is_nothrow_copy_constructible template<typename _Tp> struct is_nothrow_copy_constructible : public __is_nothrow_copy_constructible_impl<_Tp> { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_nothrow_move_constructible_impl; template<typename _Tp> struct __is_nothrow_move_constructible_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_nothrow_move_constructible_impl<_Tp, true> : public is_nothrow_constructible<_Tp, _Tp&&> { }; // 判断非抛出异常的移动的拷贝构造函数 /// is_nothrow_move_constructible template<typename _Tp> struct is_nothrow_move_constructible : public __is_nothrow_move_constructible_impl<_Tp> { }; template<typename _Tp, typename _Up> class __is_assignable_helper { template<typename _Tp1, typename _Up1, typename = decltype(declval<_Tp1>() = declval<_Up1>())> static true_type __test(int); template<typename, typename> static false_type __test(...); public: typedef decltype(__test<_Tp, _Up>(0)) type; }; // 判断是否可以赋值 /// is_assignable template<typename _Tp, typename _Up> struct is_assignable : public __is_assignable_helper<_Tp, _Up>::type { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_copy_assignable_impl; template<typename _Tp> struct __is_copy_assignable_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_copy_assignable_impl<_Tp, true> : public is_assignable<_Tp&, const _Tp&> { }; // 判断是否可以拷贝赋值 /// is_copy_assignable template<typename _Tp> struct is_copy_assignable : public __is_copy_assignable_impl<_Tp> { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_move_assignable_impl; template<typename _Tp> struct __is_move_assignable_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_move_assignable_impl<_Tp, true> : public is_assignable<_Tp&, _Tp&&> { }; // 判断是否移动赋值 /// is_move_assignable template<typename _Tp> struct is_move_assignable : public __is_move_assignable_impl<_Tp> { }; template<typename _Tp, typename _Up> struct __is_nt_assignable_impl : public integral_constant<bool, noexcept(declval<_Tp>() = declval<_Up>())> { }; // 判断非抛出异常的赋值 /// is_nothrow_assignable template<typename _Tp, typename _Up> struct is_nothrow_assignable : public __and_<is_assignable<_Tp, _Up>, __is_nt_assignable_impl<_Tp, _Up>>::type { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_nt_copy_assignable_impl; template<typename _Tp> struct __is_nt_copy_assignable_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_nt_copy_assignable_impl<_Tp, true> : public is_nothrow_assignable<_Tp&, const _Tp&> { }; // 判断非抛出异常的拷贝赋值 /// is_nothrow_copy_assignable template<typename _Tp> struct is_nothrow_copy_assignable : public __is_nt_copy_assignable_impl<_Tp> { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __is_nt_move_assignable_impl; template<typename _Tp> struct __is_nt_move_assignable_impl<_Tp, false> : public false_type { }; template<typename _Tp> struct __is_nt_move_assignable_impl<_Tp, true> : public is_nothrow_assignable<_Tp&, _Tp&&> { }; // 判断非抛出异常的移动拷贝赋值 /// is_nothrow_move_assignable template<typename _Tp> struct is_nothrow_move_assignable : public __is_nt_move_assignable_impl<_Tp> { }; // 判断是否为平凡的拷贝构造 /// is_trivially_constructible template<typename _Tp, typename... _Args> struct is_trivially_constructible : public __and_<is_constructible<_Tp, _Args...>, integral_constant<bool, __is_trivially_constructible(_Tp, _Args...)>>::type { }; // 判断是否为平凡的默认拷贝构造 /// is_trivially_default_constructible template<typename _Tp> struct is_trivially_default_constructible : public is_trivially_constructible<_Tp>::type { }; // 判断是否为平凡的拷贝构造 /// is_trivially_copy_constructible template<typename _Tp> struct is_trivially_copy_constructible : public __and_<is_copy_constructible<_Tp>, integral_constant<bool, __is_trivially_constructible(_Tp, const _Tp&)>>::type { }; // 判断是否为平凡的移动拷贝构造 /// is_trivially_move_constructible template<typename _Tp> struct is_trivially_move_constructible : public __and_<is_move_constructible<_Tp>, integral_constant<bool, __is_trivially_constructible(_Tp, _Tp&&)>>::type { }; // 判断是否为平凡的赋值 /// is_trivially_assignable template<typename _Tp, typename _Up> struct is_trivially_assignable : public __and_<is_assignable<_Tp, _Up>, integral_constant<bool, __is_trivially_assignable(_Tp, _Up)>>::type { }; // 判断是否为平凡的拷贝赋值 /// is_trivially_copy_assignable template<typename _Tp> struct is_trivially_copy_assignable : public __and_<is_copy_assignable<_Tp>, integral_constant<bool, __is_trivially_assignable(_Tp&, const _Tp&)>>::type { }; // 判断是否为平凡的移动拷贝赋值 /// is_trivially_move_assignable template<typename _Tp> struct is_trivially_move_assignable : public __and_<is_move_assignable<_Tp>, integral_constant<bool, __is_trivially_assignable(_Tp&, _Tp&&)>>::type { }; // 判断是否为平凡的析构 /// is_trivially_destructible template<typename _Tp> struct is_trivially_destructible : public __and_<is_destructible<_Tp>, integral_constant<bool, __has_trivial_destructor(_Tp)>>::type { }; // 判断是否有平凡的默认构造 /// has_trivial_default_constructor (temporary legacy) template<typename _Tp> struct has_trivial_default_constructor : public integral_constant<bool, __has_trivial_constructor(_Tp)> { } _GLIBCXX_DEPRECATED; // 判断是否有平凡的拷贝构造 /// has_trivial_copy_constructor (temporary legacy) template<typename _Tp> struct has_trivial_copy_constructor : public integral_constant<bool, __has_trivial_copy(_Tp)> { } _GLIBCXX_DEPRECATED; // 判断是否有平凡的拷贝赋值 /// has_trivial_copy_assign (temporary legacy) template<typename _Tp> struct has_trivial_copy_assign : public integral_constant<bool, __has_trivial_assign(_Tp)> { } _GLIBCXX_DEPRECATED; // 判断是否有虚析构 /// has_virtual_destructor template<typename _Tp> struct has_virtual_destructor : public integral_constant<bool, __has_virtual_destructor(_Tp)> { }; // type property queries. // 判断是否对齐 /// alignment_of template<typename _Tp> struct alignment_of : public integral_constant<std::size_t, __alignof__(_Tp)> { }; // 判断数据的秩: 可以和维度类似,例如单一类型为0, 数组为1, 二维数组为2等 /// rank template<typename> struct rank : public integral_constant<std::size_t, 0> { }; template<typename _Tp, std::size_t _Size> struct rank<_Tp[_Size]> : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { }; template<typename _Tp> struct rank<_Tp[]> : public integral_constant<std::size_t, 1 + rank<_Tp>::value> { }; // 判断是否为拓展类型, 单一类型为0, 数组返回数组的大小 /// extent template<typename, unsigned _Uint> struct extent : public integral_constant<std::size_t, 0> { }; template<typename _Tp, unsigned _Uint, std::size_t _Size> struct extent<_Tp[_Size], _Uint> : public integral_constant<std::size_t, _Uint == 0 ? _Size : extent<_Tp, _Uint - 1>::value> { }; template<typename _Tp, unsigned _Uint> struct extent<_Tp[], _Uint> : public integral_constant<std::size_t, _Uint == 0 ? 0 : extent<_Tp, _Uint - 1>::value> { }; // Type relations. // 是否相同类型,默认为false /// is_same template<typename, typename> struct is_same : public false_type { }; template<typename _Tp> struct is_same<_Tp, _Tp> : public true_type { }; // 判断是否为继承关系 /// is_base_of template<typename _Base, typename _Derived> struct is_base_of : public integral_constant<bool, __is_base_of(_Base, _Derived)> { }; // from为void/to为函数/to为数组特化 => void template<typename _From, typename _To, bool = __or_<is_void<_From>, is_function<_To>, is_array<_To>>::value> struct __is_convertible_helper { typedef typename is_void<_To>::type type; }; template<typename _From, typename _To> class __is_convertible_helper<_From, _To, false> { template<typename _To1> static void __test_aux(_To1); // 这里判断From1的变量能否作为__test_aux(To1)的参数,如果可以,则可以进行类型转换 template<typename _From1, typename _To1, typename = decltype(__test_aux<_To1>(std::declval<_From1>()))> static true_type __test(int); template<typename, typename> static false_type __test(...); public: typedef decltype(__test<_From, _To>(0)) type; }; // 判断是否可以进行类型转换 /// is_convertible template<typename _From, typename _To> struct is_convertible : public __is_convertible_helper<_From, _To>::type { }; // Const-volatile modifications. // 移除const修饰 /// remove_const template<typename _Tp> struct remove_const { typedef _Tp type; }; template<typename _Tp> struct remove_const<_Tp const> { typedef _Tp type; }; // 移除volatile修饰 /// remove_volatile template<typename _Tp> struct remove_volatile { typedef _Tp type; }; template<typename _Tp> struct remove_volatile<_Tp volatile> { typedef _Tp type; }; // 移除const + volatile修饰 /// remove_cv template<typename _Tp> struct remove_cv { typedef typename remove_const<typename remove_volatile<_Tp>::type>::type type; }; // 添加const修饰 /// add_const template<typename _Tp> struct add_const { typedef _Tp const type; }; // 添加volatile修饰 /// add_volatile template<typename _Tp> struct add_volatile { typedef _Tp volatile type; }; // 添加const + volatile修饰 /// add_cv template<typename _Tp> struct add_cv { typedef typename add_const<typename add_volatile<_Tp>::type>::type type; }; #if __cplusplus > 201103L #define __cpp_lib_transformation_trait_aliases 201304 /// Alias template for remove_const template<typename _Tp> using remove_const_t = typename remove_const<_Tp>::type; /// Alias template for remove_volatile template<typename _Tp> using remove_volatile_t = typename remove_volatile<_Tp>::type; /// Alias template for remove_cv template<typename _Tp> using remove_cv_t = typename remove_cv<_Tp>::type; /// Alias template for add_const template<typename _Tp> using add_const_t = typename add_const<_Tp>::type; /// Alias template for add_volatile template<typename _Tp> using add_volatile_t = typename add_volatile<_Tp>::type; /// Alias template for add_cv template<typename _Tp> using add_cv_t = typename add_cv<_Tp>::type; #endif // Reference transformations. // 移除引用(包含左值引用+右值引用) /// remove_reference template<typename _Tp> struct remove_reference { typedef _Tp type; }; template<typename _Tp> struct remove_reference<_Tp&> { typedef _Tp type; }; template<typename _Tp> struct remove_reference<_Tp&&> { typedef _Tp type; }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __add_lvalue_reference_helper { typedef _Tp type; }; template<typename _Tp> struct __add_lvalue_reference_helper<_Tp, true> { typedef _Tp& type; }; // 添加左值引用 /// add_lvalue_reference template<typename _Tp> struct add_lvalue_reference : public __add_lvalue_reference_helper<_Tp> { }; template<typename _Tp, bool = __is_referenceable<_Tp>::value> struct __add_rvalue_reference_helper { typedef _Tp type; }; template<typename _Tp> struct __add_rvalue_reference_helper<_Tp, true> { typedef _Tp&& type; }; // 添加右值引用 /// add_rvalue_reference template<typename _Tp> struct add_rvalue_reference : public __add_rvalue_reference_helper<_Tp> { }; #if __cplusplus > 201103L /// Alias template for remove_reference template<typename _Tp> using remove_reference_t = typename remove_reference<_Tp>::type; /// Alias template for add_lvalue_reference template<typename _Tp> using add_lvalue_reference_t = typename add_lvalue_reference<_Tp>::type; /// Alias template for add_rvalue_reference template<typename _Tp> using add_rvalue_reference_t = typename add_rvalue_reference<_Tp>::type; #endif // Sign modifications. // Utility for constructing identically cv-qualified types. // 添加 const + volatile 修饰 template<typename _Unqualified, bool _IsConst, bool _IsVol> struct __cv_selector; template<typename _Unqualified> struct __cv_selector<_Unqualified, false, false> { typedef _Unqualified __type; }; template<typename _Unqualified> struct __cv_selector<_Unqualified, false, true> { typedef volatile _Unqualified __type; }; template<typename _Unqualified> struct __cv_selector<_Unqualified, true, false> { typedef const _Unqualified __type; }; template<typename _Unqualified> struct __cv_selector<_Unqualified, true, true> { typedef const volatile _Unqualified __type; }; // 根据Qualified的属性,对Unqulified选择添加的属性,保证添加: const / volatile / const + volatile / 无添加 template<typename _Qualified, typename _Unqualified, bool _IsConst = is_const<_Qualified>::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. // 对整型添加unsigned修饰 template<typename _Tp> struct __make_unsigned { typedef _Tp __type; }; template<> struct __make_unsigned<char> { typedef unsigned char __type; }; template<> struct __make_unsigned<signed char> { typedef unsigned char __type; }; template<> struct __make_unsigned<short> { typedef unsigned short __type; }; template<> struct __make_unsigned<int> { typedef unsigned int __type; }; template<> struct __make_unsigned<long> { typedef unsigned long __type; }; template<> struct __make_unsigned<long long> { typedef unsigned long long __type; }; #if defined(_GLIBCXX_USE_WCHAR_T) && !defined(__WCHAR_UNSIGNED__) template<> struct __make_unsigned<wchar_t> : __make_unsigned<__WCHAR_TYPE__> { }; #endif #if defined(__GLIBCXX_TYPE_INT_N_0) template<> struct __make_unsigned<__GLIBCXX_TYPE_INT_N_0> { typedef unsigned __GLIBCXX_TYPE_INT_N_0 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_1) template<> struct __make_unsigned<__GLIBCXX_TYPE_INT_N_1> { typedef unsigned __GLIBCXX_TYPE_INT_N_1 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_2) template<> struct __make_unsigned<__GLIBCXX_TYPE_INT_N_2> { typedef unsigned __GLIBCXX_TYPE_INT_N_2 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_3) template<> struct __make_unsigned<__GLIBCXX_TYPE_INT_N_3> { typedef unsigned __GLIBCXX_TYPE_INT_N_3 __type; }; #endif // Select between integral and enum: not possible to be both. // 在整型和枚举型之间的选择 template<typename _Tp, bool _IsInt = is_integral<_Tp>::value, bool _IsEnum = is_enum<_Tp>::value> class __make_unsigned_selector; // 整形时 => unsigned 整型 template<typename _Tp> class __make_unsigned_selector<_Tp, true, false> { typedef __make_unsigned<typename remove_cv<_Tp>::type> __unsignedt; typedef typename __unsignedt::__type __unsigned_type; typedef __match_cv_qualifiers<_Tp, __unsigned_type> __cv_unsigned; public: typedef typename __cv_unsigned::__type __type; }; // 枚举型时 => unsigned 整型 template<typename _Tp> 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<typename _Tp> struct make_unsigned { typedef typename __make_unsigned_selector<_Tp>::__type type; }; // 特化布尔类型 // Integral, but don't define. template<> struct make_unsigned<bool>; // Utility for finding the signed versions of unsigned integral types. // 整型添加signed 修饰 template<typename _Tp> struct __make_signed { typedef _Tp __type; }; template<> struct __make_signed<char> { typedef signed char __type; }; template<> struct __make_signed<unsigned char> { typedef signed char __type; }; template<> struct __make_signed<unsigned short> { typedef signed short __type; }; template<> struct __make_signed<unsigned int> { typedef signed int __type; }; template<> struct __make_signed<unsigned long> { typedef signed long __type; }; template<> struct __make_signed<unsigned long long> { typedef signed long long __type; }; #if defined(_GLIBCXX_USE_WCHAR_T) && defined(__WCHAR_UNSIGNED__) template<> struct __make_signed<wchar_t> : __make_signed<__WCHAR_TYPE__> { }; #endif #ifdef _GLIBCXX_USE_C99_STDINT_TR1 template<> struct __make_signed<char16_t> : __make_signed<uint_least16_t> { }; template<> struct __make_signed<char32_t> : __make_signed<uint_least32_t> { }; #endif #if defined(__GLIBCXX_TYPE_INT_N_0) template<> struct __make_signed<unsigned __GLIBCXX_TYPE_INT_N_0> { typedef __GLIBCXX_TYPE_INT_N_0 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_1) template<> struct __make_signed<unsigned __GLIBCXX_TYPE_INT_N_1> { typedef __GLIBCXX_TYPE_INT_N_1 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_2) template<> struct __make_signed<unsigned __GLIBCXX_TYPE_INT_N_2> { typedef __GLIBCXX_TYPE_INT_N_2 __type; }; #endif #if defined(__GLIBCXX_TYPE_INT_N_3) template<> struct __make_signed<unsigned __GLIBCXX_TYPE_INT_N_3> { typedef __GLIBCXX_TYPE_INT_N_3 __type; }; #endif // Select between integral and enum: not possible to be both. template<typename _Tp, bool _IsInt = is_integral<_Tp>::value, bool _IsEnum = is_enum<_Tp>::value> class __make_signed_selector; template<typename _Tp> class __make_signed_selector<_Tp, true, false> { typedef __make_signed<typename remove_cv<_Tp>::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<typename _Tp> 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<typename _Tp> struct make_signed { typedef typename __make_signed_selector<_Tp>::__type type; }; // Integral, but don't define. template<> struct make_signed<bool>; #if __cplusplus > 201103L /// Alias template for make_signed template<typename _Tp> using make_signed_t = typename make_signed<_Tp>::type; /// Alias template for make_unsigned template<typename _Tp> using make_unsigned_t = typename make_unsigned<_Tp>::type; #endif // Array modifications. // 获取Tp/T[N]/Tp[]的类型 /// remove_extent template<typename _Tp> struct remove_extent { typedef _Tp type; }; template<typename _Tp, std::size_t _Size> struct remove_extent<_Tp[_Size]> { typedef _Tp type; }; template<typename _Tp> struct remove_extent<_Tp[]> { typedef _Tp type; }; // 获取Tp/T[N]/Tp[]的类型: 如果Tp是数组时,还会递归获取 /// remove_all_extents template<typename _Tp> struct remove_all_extents { typedef _Tp type; }; template<typename _Tp, std::size_t _Size> struct remove_all_extents<_Tp[_Size]> { typedef typename remove_all_extents<_Tp>::type type; }; template<typename _Tp> struct remove_all_extents<_Tp[]> { typedef typename remove_all_extents<_Tp>::type type; }; #if __cplusplus > 201103L /// Alias template for remove_extent template<typename _Tp> using remove_extent_t = typename remove_extent<_Tp>::type; /// Alias template for remove_all_extents template<typename _Tp> using remove_all_extents_t = typename remove_all_extents<_Tp>::type; #endif // Pointer modifications. template<typename _Tp, typename> struct __remove_pointer_helper { typedef _Tp type; }; template<typename _Tp, typename _Up> struct __remove_pointer_helper<_Tp, _Up*> { typedef _Up type; }; // 获取指针的类型 /// remove_pointer template<typename _Tp> struct remove_pointer : public __remove_pointer_helper<_Tp, typename remove_cv<_Tp>::type> { }; // 添加指针类型 /// add_pointer template<typename _Tp, bool = __or_<__is_referenceable<_Tp>, is_void<_Tp>>::value> struct __add_pointer_helper { typedef _Tp type; }; template<typename _Tp> struct __add_pointer_helper<_Tp, true> { typedef typename remove_reference<_Tp>::type* type; }; template<typename _Tp> struct add_pointer : public __add_pointer_helper<_Tp> { }; #if __cplusplus > 201103L /// Alias template for remove_pointer template<typename _Tp> using remove_pointer_t = typename remove_pointer<_Tp>::type; /// Alias template for add_pointer template<typename _Tp> using add_pointer_t = typename add_pointer<_Tp>::type; #endif // 按照Len对齐 template<std::size_t _Len> 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. */ // 按照Len内存对齐 template<std::size_t _Len, std::size_t _Align = __alignof__(typename __aligned_storage_msa<_Len>::__type)> struct aligned_storage { union type { unsigned char __data[_Len]; struct __attribute__((__aligned__((_Align)))) { } __align; }; }; template <typename... _Types> struct __strictest_alignment { static const size_t _S_alignment = 0; static const size_t _S_size = 0; }; template <typename _Tp, typename... _Types> struct __strictest_alignment<_Tp, _Types...> { static const size_t _S_alignment = alignof(_Tp) > __strictest_alignment<_Types...>::_S_alignment ? alignof(_Tp) : __strictest_alignment<_Types...>::_S_alignment; static const size_t _S_size = sizeof(_Tp) > __strictest_alignment<_Types...>::_S_size ? sizeof(_Tp) : __strictest_alignment<_Types...>::_S_size; }; /** * @brief Provide aligned storage for types. * * [meta.trans.other] * * Provides aligned storage for any of the provided types of at * least size _Len. * * @see aligned_storage */ // 按照Len对齐Type template <size_t _Len, typename... _Types> struct aligned_union { private: static_assert(sizeof...(_Types) != 0, "At least one type is required"); using __strictest = __strictest_alignment<_Types...>; static const size_t _S_len = _Len > __strictest::_S_size ? _Len : __strictest::_S_size; public: /// The value of the strictest alignment of _Types. static const size_t alignment_value = __strictest::_S_alignment; /// The storage. typedef typename aligned_storage<_S_len, alignment_value>::type type; }; template <size_t _Len, typename... _Types> const size_t aligned_union<_Len, _Types...>::alignment_value; // Decay trait for arrays and functions, used for perfect forwarding // in make_pair, make_tuple, etc. template<typename _Up, bool _IsArray = is_array<_Up>::value, bool _IsFunction = is_function<_Up>::value> struct __decay_selector; // NB: DR 705. template<typename _Up> struct __decay_selector<_Up, false, false> { typedef typename remove_cv<_Up>::type __type; }; template<typename _Up> struct __decay_selector<_Up, true, false> { typedef typename remove_extent<_Up>::type* __type; }; template<typename _Up> struct __decay_selector<_Up, false, true> { typedef typename add_pointer<_Up>::type __type; }; // 非数组 & 非函数: 移除引用 + 移除const + volatile修饰 // 数组: 移除引用 + 获取数组第一层的类型 // 函数: 移除引用 + 添加指针类型 /// decay template<typename _Tp> class decay { typedef typename remove_reference<_Tp>::type __remove_type; public: typedef typename __decay_selector<__remove_type>::__type type; }; template<typename _Tp> class reference_wrapper; // Helper which adds a reference to a type when given a reference_wrapper template<typename _Tp> struct __strip_reference_wrapper { typedef _Tp __type; }; template<typename _Tp> struct __strip_reference_wrapper<reference_wrapper<_Tp> > { typedef _Tp& __type; }; template<typename _Tp> struct __decay_and_strip { typedef typename __strip_reference_wrapper< typename decay<_Tp>::type>::__type __type; }; // 条件为true时,定义类型 // Primary template. /// Define a member typedef @c type only if a boolean constant is true. template<bool, typename _Tp = void> struct enable_if { }; // Partial specialization for true. template<typename _Tp> struct enable_if<true, _Tp> { typedef _Tp type; }; // 多个条件 template<typename... _Cond> using _Require = typename enable_if<__and_<_Cond...>::value>::type; // 条件为true时,获取Iftrue的类型; false时,获取Iffalse的类型 // Primary template. /// Define a member typedef @c type to one of two argument types. template<bool _Cond, typename _Iftrue, typename _Iffalse> struct conditional { typedef _Iftrue type; }; // Partial specialization for false. template<typename _Iftrue, typename _Iffalse> struct conditional<false, _Iftrue, _Iffalse> { typedef _Iffalse type; }; /// common_type template<typename... _Tp> struct common_type; // Sfinae-friendly common_type implementation: struct __do_common_type_impl { template<typename _Tp, typename _Up> static __success_type<typename decay<decltype (true ? std::declval<_Tp>() : std::declval<_Up>())>::type> _S_test(int); template<typename, typename> static __failure_type _S_test(...); }; template<typename _Tp, typename _Up> struct __common_type_impl : private __do_common_type_impl { typedef decltype(_S_test<_Tp, _Up>(0)) type; }; struct __do_member_type_wrapper { template<typename _Tp> static __success_type<typename _Tp::type> _S_test(int); template<typename> static __failure_type _S_test(...); }; template<typename _Tp> struct __member_type_wrapper : private __do_member_type_wrapper { typedef decltype(_S_test<_Tp>(0)) type; }; template<typename _CTp, typename... _Args> struct __expanded_common_type_wrapper { typedef common_type<typename _CTp::type, _Args...> type; }; template<typename... _Args> struct __expanded_common_type_wrapper<__failure_type, _Args...> { typedef __failure_type type; }; // 获取所有类型都兼容的类型 template<typename _Tp> struct common_type<_Tp> { typedef typename decay<_Tp>::type type; }; template<typename _Tp, typename _Up> struct common_type<_Tp, _Up> : public __common_type_impl<_Tp, _Up>::type { }; template<typename _Tp, typename _Up, typename... _Vp> struct common_type<_Tp, _Up, _Vp...> : public __expanded_common_type_wrapper<typename __member_type_wrapper< common_type<_Tp, _Up>>::type, _Vp...>::type { }; // 枚举型的底层类型 /// The underlying type of an enum. template<typename _Tp> struct underlying_type { typedef __underlying_type(_Tp) type; }; template<typename _Tp> struct __declval_protector { static const bool __stop = false; static typename add_rvalue_reference<_Tp>::type __delegate(); }; // declval()函数,用于模板推导, 实际上就是返回Tp的右值引用 template<typename _Tp> 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<typename _Signature> class result_of; // Sfinae-friendly result_of implementation: #define __cpp_lib_result_of_sfinae 201210 // [func.require] paragraph 1 bullet 1: struct __result_of_memfun_ref_impl { template<typename _Fp, typename _Tp1, typename... _Args> static __success_type<decltype( (std::declval<_Tp1>().*std::declval<_Fp>())(std::declval<_Args>()...) )> _S_test(int); template<typename...> static __failure_type _S_test(...); }; template<typename _MemPtr, typename _Arg, typename... _Args> struct __result_of_memfun_ref : private __result_of_memfun_ref_impl { typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type; }; // [func.require] paragraph 1 bullet 2: struct __result_of_memfun_deref_impl { template<typename _Fp, typename _Tp1, typename... _Args> static __success_type<decltype( ((*std::declval<_Tp1>()).*std::declval<_Fp>())(std::declval<_Args>()...) )> _S_test(int); template<typename...> static __failure_type _S_test(...); }; template<typename _MemPtr, typename _Arg, typename... _Args> struct __result_of_memfun_deref : private __result_of_memfun_deref_impl { typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type; }; // [func.require] paragraph 1 bullet 3: struct __result_of_memobj_ref_impl { template<typename _Fp, typename _Tp1> static __success_type<decltype( std::declval<_Tp1>().*std::declval<_Fp>() )> _S_test(int); template<typename, typename> static __failure_type _S_test(...); }; template<typename _MemPtr, typename _Arg> struct __result_of_memobj_ref : private __result_of_memobj_ref_impl { typedef decltype(_S_test<_MemPtr, _Arg>(0)) type; }; // [func.require] paragraph 1 bullet 4: struct __result_of_memobj_deref_impl { template<typename _Fp, typename _Tp1> static __success_type<decltype( (*std::declval<_Tp1>()).*std::declval<_Fp>() )> _S_test(int); template<typename, typename> static __failure_type _S_test(...); }; template<typename _MemPtr, typename _Arg> struct __result_of_memobj_deref : private __result_of_memobj_deref_impl { typedef decltype(_S_test<_MemPtr, _Arg>(0)) type; }; template<typename _MemPtr, typename _Arg> struct __result_of_memobj; template<typename _Res, typename _Class, typename _Arg> struct __result_of_memobj<_Res _Class::*, _Arg> { typedef typename remove_cv<typename remove_reference< _Arg>::type>::type _Argval; typedef _Res _Class::* _MemPtr; typedef typename conditional<__or_<is_same<_Argval, _Class>, is_base_of<_Class, _Argval>>::value, __result_of_memobj_ref<_MemPtr, _Arg>, __result_of_memobj_deref<_MemPtr, _Arg> >::type::type type; }; template<typename _MemPtr, typename _Arg, typename... _Args> struct __result_of_memfun; template<typename _Res, typename _Class, typename _Arg, typename... _Args> struct __result_of_memfun<_Res _Class::*, _Arg, _Args...> { typedef typename remove_cv<typename remove_reference< _Arg>::type>::type _Argval; typedef _Res _Class::* _MemPtr; typedef typename conditional<__or_<is_same<_Argval, _Class>, is_base_of<_Class, _Argval>>::value, __result_of_memfun_ref<_MemPtr, _Arg, _Args...>, __result_of_memfun_deref<_MemPtr, _Arg, _Args...> >::type::type type; }; template<bool, bool, typename _Functor, typename... _ArgTypes> struct __result_of_impl { typedef __failure_type type; }; template<typename _MemPtr, typename _Arg> struct __result_of_impl<true, false, _MemPtr, _Arg> : public __result_of_memobj<typename decay<_MemPtr>::type, _Arg> { }; template<typename _MemPtr, typename _Arg, typename... _Args> struct __result_of_impl<false, true, _MemPtr, _Arg, _Args...> : public __result_of_memfun<typename decay<_MemPtr>::type, _Arg, _Args...> { }; // [func.require] paragraph 1 bullet 5: struct __result_of_other_impl { template<typename _Fn, typename... _Args> static __success_type<decltype( std::declval<_Fn>()(std::declval<_Args>()...) )> _S_test(int); template<typename...> static __failure_type _S_test(...); }; template<typename _Functor, typename... _ArgTypes> struct __result_of_impl<false, false, _Functor, _ArgTypes...> : private __result_of_other_impl { typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type; }; // 函数的返回值类型 template<typename _Functor, typename... _ArgTypes> struct result_of<_Functor(_ArgTypes...)> : public __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 { }; #if __cplusplus > 201103L /// Alias template for aligned_storage template<size_t _Len, size_t _Align = __alignof__(typename __aligned_storage_msa<_Len>::__type)> using aligned_storage_t = typename aligned_storage<_Len, _Align>::type; template <size_t _Len, typename... _Types> using aligned_union_t = typename aligned_union<_Len, _Types...>::type; /// Alias template for decay template<typename _Tp> using decay_t = typename decay<_Tp>::type; /// Alias template for enable_if template<bool _Cond, typename _Tp = void> using enable_if_t = typename enable_if<_Cond, _Tp>::type; /// Alias template for conditional template<bool _Cond, typename _Iftrue, typename _Iffalse> using conditional_t = typename conditional<_Cond, _Iftrue, _Iffalse>::type; /// Alias template for common_type template<typename... _Tp> using common_type_t = typename common_type<_Tp...>::type; /// Alias template for underlying_type template<typename _Tp> using underlying_type_t = typename underlying_type<_Tp>::type; /// Alias template for result_of template<typename _Tp> using result_of_t = typename result_of<_Tp>::type; #endif template<typename...> using __void_t = void; /// @} group metaprogramming /** * Use SFINAE to determine if the type _Tp has a publicly-accessible * member type _NTYPE. */ #define _GLIBCXX_HAS_NESTED_TYPE(_NTYPE) \ template<typename _Tp, typename = __void_t<>> \ struct __has_##_NTYPE \ : false_type \ { }; \ template<typename _Tp> \ struct __has_##_NTYPE<_Tp, __void_t<typename _Tp::_NTYPE>> \ : true_type \ { }; _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++11 #endif // _GLIBCXX_TYPE_TRAITS
这篇关于C++11 元编程学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-12深入理解 ECMAScript 2024 新特性:Map.groupBy() 分组操作
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势