[C ++] Type_traits header file source code analysis (1)

Constant type:

/// integral_constant
template<typename _Tp, _Tp __v>
struct integral_constant
{
	static const _Tp                      value = __v;
	typedef _Tp                           value_type;
	typedef integral_constant<_Tp, __v>   type;
};
  
/// typedef for true_type
typedef integral_constant<bool, true>     true_type;

/// typedef for false_type
typedef integral_constant<bool, false>    false_type;

Remove references:

template<typename _Tp>
struct remove_reference
{ typedef _Tp     type; };

template<typename _Tp>
struct remove_reference<_Tp&>
{ typedef _Tp     type; };

Remove constants:

template<typename _Tp>
struct remove_const
{ typedef _Tp     type; };

template<typename _Tp>
struct remove_const<_Tp const>
{ typedef _Tp     type; };

Determine whether the types are equal:

template<typename, typename>
struct is_same
: public false_type { };

template<typename _Tp>
struct is_same<_Tp, _Tp>
: public true_type { };

Determine whether the type is constant:

template<typename>
struct is_const
: public false_type { };

template<typename _Tp>
struct is_const<_Tp const>
: public true_type { };

Determine whether it is volatile type:

template<typename>
struct is_volatile
: public false_type { };

template<typename _Tp>
struct is_volatile<_Tp volatile>
: public true_type { };

Remove const and volatile attributes:

template<typename _Tp>
struct remove_cv
{
	typedef typename
	remove_const<typename remove_volatile<_Tp>::type>::type     type;
};

Add a pointer to the type:

template<typename _Tp>
struct add_pointer
{ typedef typename remove_reference<_Tp>::type*     type; };
Published 401 original articles · Liked 14 · Visits 100,000+

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105446421