[C ++] Análisis de código fuente del archivo de encabezado Type_traits (1)

Tipo constante:

/// 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;

Eliminar referencias:

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

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

Eliminar constantes:

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

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

Determine si los tipos son iguales:

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

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

Determine si el tipo es constante:

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

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

Determine si es de tipo volátil:

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

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

Eliminar const y atributos volátiles:

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

Agregue un puntero al tipo:

template<typename _Tp>
struct add_pointer
{ typedef typename remove_reference<_Tp>::type*     type; };
401 artículos originales publicados · Me gusta 14 · Visitas 100,000+

Supongo que te gusta

Origin blog.csdn.net/LU_ZHAO/article/details/105446421
Recomendado
Clasificación