0
点赞
收藏
分享

微信扫一扫

C++【type traits】 | 基本使用

村里搬砖的月野兔 2022-04-18 阅读 21

文章目录

一、助手类

1、integral_constant

/** 模板参数一个为类型,一个为value */
template<typename _Tp, _Tp __v>
struct integral_constant
{
    static constexpr _Tp                  value = __v;		/* 将数值传给value */
    typedef _Tp                           value_type;		/* 定义类型 */
    typedef integral_constant<_Tp, __v>   type;				/* 自身类型 */
    /* 仿函数,执行时即可返回传入的数值 */
    constexpr value_type operator()() const noexcept { return value; }
};

案例

该案例为一个递归的模板用法;
/** 递归函数 */
template <unsigned n>
struct factorial : std::integral_constant<int, n * factorial<n-1>::value> {};

/** 当传入数值为0时,执行空函数,即为终止条件 */
template <>
struct factorial<0> : std::integral_constant<int,1> {};

int main() {
    std::cout << factorial<5>::value;  
    return 0;
}

2、true_type和false_type

即integral_constant的实例化;
typedef integral_constant<bool, true>     true_type;
typedef integral_constant<bool, false>     false_type;

二、主要类型特征

1、主要类型类别

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 { };

在这里插入图片描述

is_class:是非联合类

is_enum:是枚举

is_floating_point:是浮点数

is_function:是函数

/** 当不满足时,函数模板没有匹配到时为false_type */
template<typename>
   struct is_function
   : public false_type { };
   
#define _GLIBCXX_NOEXCEPT_PARM , bool _NE
#define _GLIBCXX_NOEXCEPT_QUAL noexcept (_NE)
/** 满足以下的模板即为函数 */
template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes...) /const/volatile/const volatile _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes...)  /const/volatile/const volatile & _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes...)  /const/volatile/const volatile && _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes......)  /const/volatile/const volatile _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes......)  /const/volatile/const volatile & _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

template<typename _Res, typename... _ArgTypes _GLIBCXX_NOEXCEPT_PARM>
  struct is_function<_Res(_ArgTypes......)  /const/volatile/const volatile && _GLIBCXX_NOEXCEPT_QUAL>
  : public true_type { };

is_integral:是不可分割的

is_lvalue_reference:是左值引用

is_member_function_pointer:是成员函数指针

is_member_object_pointer:是成员对象指针

is_pointer:是指针

is_rvalue_reference:是右值引用

is_union:是联合

is_void:是空

/** 取出const */
template<typename _Tp>
  struct remove_const
  { typedef _Tp     type; };

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

/** 去除volatile */
template<typename _Tp>
  struct remove_volatile
  { typedef _Tp     type; };

template<typename _Tp>
  struct remove_volatile<_Tp volatile>
  { typedef _Tp     type; };

/** 去除const和volatile */
template<typename _Tp>
  struct remove_cv
  {
    typedef typename
    remove_const<typename remove_volatile<_Tp>::type>::type     type;
  };

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

template<>
  struct __is_void_helper<void>
  : public true_type { };

/// is_void
template<typename _Tp>
  struct is_void
  : public __is_void_helper<typename remove_cv<_Tp>::type>::type
  { };

在这里插入图片描述

2、复合类型分类

is_arithmetic:是算数类型

is_compound:是复合类型

is_fundamental:是基本类型

is_member_pointer:是成员指针类型

is_object:是对象类型

is_reference:是引用类型

is_scalar:是标量类型

3、类型属性

is_abstract:是抽象类型

is_const:是const限定

is_empty:是基本类型

is_literal_type:是成员指针类型

is_pod:是pod类型

is_polymorphic:是多态类型

is_signed:是符号类型

is_standard_layout:是标准布局类型

is_trivial:是无用类型

is_trivially_copyable:是无用复制类型

is_unsigned:是无符号类型

is_volatile:是volatile限定

4、类型特征

has_virtual_destructor:有虚析构函数

is_assignable:可赋值

is_constructible:可构造

is_copy_assignable:可拷贝赋值

is_copy_constructible:是拷贝构造

is_destructible:是析构

is_default_constructible:是默认析构

is_move_assignable:是移动赋值

is_move_constructible:是移动构造

is_trivially_assignable:无用的赋值

is_trivially_constructible:无用的构造函数

is_trivially_copy_assignable:无用的拷贝构造函数

is_trivially_copy_constructible:无用的拷贝构造函数

is_trivially_destructible:无用的析构函数

is_trivially_default_constructible:无用的默认构造函数

is_trivially_move_assignable:无用的移动赋值

is_trivially_move_constructible:无用的移动构造

is_nothrow_assignable:不抛异常的赋值

is_nothrow_constructible:不抛异常的构造

is_nothrow_copy_assignable:不抛异常的拷贝赋值

is_nothrow_copy_constructible:不抛异常的拷贝构造

is_nothrow_destructible:不抛异常的析构

is_nothrow_default_constructible:不抛异常的默认构造

is_nothrow_move_assignable:不抛异常的移动赋值

is_nothrow_move_constructible:不抛异常的移动构造

5、类型关系

is_base_of:是基类

is_convertible:是可转换

is_same:是同一类型

6、属性查询

alignment_of:对齐方式

extent:数组维度范围

rank:数组等级

举报

相关推荐

0 条评论