Template Meta programming reference

sizeof... operator

查询参数包(parameter pack)中的元素数量。

语法
sizeof...( parametet pack ) // (since c++11)

std::decay

定义于头文件<type_traits>

template< class T >
struct decay;

Applies lvalue-to-rvalue, array-to-pointer, and function-to-pointer implicit conversions to the type T, removes cv-qualifiers, and defines the resulting type as the member typedef type. Formally:

If T names the type “array of U” or “reference to array of U”, the member typedef type is U*.
Otherwise, if T is a function type F or a reference thereto, the member typedef type is std::add_pointer::type.
Otherwise, the member typedef type is std::remove_cv<std::remove_reference::type>::type.
These conversions model the type conversion applied to all function arguments when passed by value.

Member types

Name | Definition
type | the result of applying the decay type conversions to T

Helper types
template< class T >
using decay_t = typename decay<T>::type; // (since c++14)
Possible Implementation
template< class T >
struct decay {
private:
	typedef typename std::remove_reference<T>::type U;
public:
	typedef typename std::conditional<
		std::is_array<U>::value, 
		typename std::remove_extent<U>::type*typename std::conditional<
			std::is_function<U>::value,
			typename std::add_pointer<U>::type,
			typename std::remove_cv<U>::type
		>::type
	>::type type;
		
}

std::conditional

定义于头文件<type_traits>

template< bool B, class T, class F >
struct conditional; 					// (since c++11)

定义typedef类型的成员 type,如果B == true,定义 type 为 T,如果 B == false,定义 type 为 F。

Helper types
template < bool B, class T, class F >
using conditional_t = std::conditional<B, T, F>::type;
Possible Implementation
template< bool B, class T, class F >
struct conditional { typedef T type; };

template< class T, class F >
struct conditional< false, T, F > { typedef F type; }

std::is_array

定义在头文件<type_traits>中

template< class T >
struct is_array;		// (since c++11)

校验T是不是数组类型。提供一个成员常量,如果是数组类型,其值为true,否则为false。

Helper variable template
template< class T >
inline constexpr bool is_array_v = is_array<T>::value; 	// (since C++17)
Possible Implementation
template <class T>
struct is_array : std::false_type {};

template <class T>
struct is_array<T[]> : std::true_type {};

template <class T, std::size_t N>
struct is_array<T[N]> : std::true_type {};

std::integral_constant

定义在头文件<type_traits>中

template< class T, T v >
struct integral_constant;

包装特定类型的静态常量,是C++类型特征的基类。

true_type std::integral_constant<bool, true>
false_type std::integral_constant<bool, false>

Possible Implementation

template<class T, T v>
struct integral_constant {
    static constexpr T value = v;
    using value_type = T;
    using type = integral_constant; // 使用注入类名
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; } // C++14 起
};

std::remove_extent

定义于头文件<type_traits>

template< class T >
struct remove_extent;	// (since c++11)

返回数组的类型std::remove_extent::type。注意,如果T是多维数组,仅移除第一个纬度。

Helper types
template< class T >
using remove_extent_t = typename remove_extent<T>::type; // (since c++14)
Possible Implementation
template< class T >
struct remove_extent { typedef T type; };

template< class T >
struct remove_extent<T[]> { typedef T type };

template<class T, std::size_t N>
struct remove_extent<T[N]> { typedef T type; }; 

std::is_function

定义于头文件<type_traits>

template< class T >
struct is_function;

校验T是否是函数类型 (function type)。像std::function, lambdas, 定义了operator () 操作符的类 和 函数指针 并不记做函数类型 (function type)。

它们是函数对象(function object),或称为函数符(functor)。

提供成员常量,当T是函数类型时,值为true,否则为false。

Helper variable template
template< class T >
inline constexpr bool is_function_v = is_function<T>::value; 	// (since C++17)
Possible Implementation
// primary template
template<class>
struct is_function : std::false_type { };
 
// specialization for regular functions
template<class Ret, class... Args>
struct is_function<Ret(Args...)> : std::true_type {};
 
// specialization for variadic functions such as std::printf
template<class Ret, class... Args>
struct is_function<Ret(Args......)> : std::true_type {};
 
// specialization for function types that have cv-qualifiers
template<class Ret, class... Args>
struct is_function<Ret(Args...) const> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile> : std::true_type {};
 
// specialization for function types that have ref-qualifiers
template<class Ret, class... Args>
struct is_function<Ret(Args...) &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile &> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile &&> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile &&> : std::true_type {};
 
// specializations for noexcept versions of all the above (C++17 and later)
 
template<class Ret, class... Args>
struct is_function<Ret(Args...) noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile & noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) volatile && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args...) const volatile && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) volatile && noexcept> : std::true_type {};
template<class Ret, class... Args>
struct is_function<Ret(Args......) const volatile && noexcept> : std::true_type {};

std::add_pointer

定义于头文件 <type_traits>

template< class T >
struct add_pointer; 		// (since c++11)

如果T是引用类型,则提供一个成员typedef类型,它是一个指向类型的指针。
否则,如果T一个对象类型,一个无 cv- 或 ref-qualified 的函数类型, 或一个( 可能 cv-qualified)void
类型,提供member typedef type ,其类型为 T*。
否则(如果,T是一个 cv- 或 ref-qualified 的函数类型),提供成员 typedef 类型 type, 它是 type T。

Member types

type | pointer to T or to the type referenced by T

Helper types
template< class T >
using add_pointer_t = typename add_pointer<T>::type; // (since C++ 14)
Possible implementation
namespace detail {
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since c++20)

template <class T>
auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>;
template <class T>
auto try_add_pointer(...) -> type_identity<T>;
} // namespace detail

template <class T>
struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值