武安网站建设,多用户商城系统开发多少钱,怎样自己做刷赞网站,口碑好网站建设费用一、type_traits源码介绍
1、type_traits是C11提供的模板元基础库。 2、type_traits可实现在编译期计算。包括添加修饰、萃取、判断查询、类型推导等等功能。 3、type_traits提供了编译期的true和false。
二、type_traits的作用
1、根据不同类型#xff0c;模板匹配不同版本…一、type_traits源码介绍
1、type_traits是C11提供的模板元基础库。 2、type_traits可实现在编译期计算。包括添加修饰、萃取、判断查询、类型推导等等功能。 3、type_traits提供了编译期的true和false。
二、type_traits的作用
1、根据不同类型模板匹配不同版本的算法 STL中的Algorithm通过Iterator存取Container内容Functor可以协助Algorithm完全不同的策略变化。此变化请参见C模板编程之类型萃取 惊鸿一瞥
2、编译检查模板类型复合预期 标准库经典示例C11标准库thread构造函数浅析
三、type_traits源码剖析
1、添加修饰
std::add_const
std::add_volatile
std::add_cv //同时添加const和volatile
std::add_lvalue_reference //添加左值引用
std::add_rvalue_reference //添加右值引用
std::declval //添加右值引用
std::add_pointer //先退化左、右值引用再添加指针*使用示例
std::add_constint::type t 0; //t为const int类型
//std::add_constint::type t2; //编译不通过const定义时需要初始化
2、萃取
remove_const
remove_volatile
remove_cv
remove_reference //退化左、右值引用
remove_pointer
remove_extent //数组类型退化一个维度
remove_all_extents //数组类型退化所有维度
decay //退化、衰弱复合运算const、volatile、reference、extent
auto和decltype //根据值自动推导类型。1auto和decltype 详细用法参见C11新特性auto和decltype
2type_traits std::decay(朽化 对于普通类型移除引用和cv符(const和volatile)规则如下 移除T类型的引用得到类型UU为remove_reference T ::type 如果is_array U ::value为真修改类型为 remove_reference U ::type* 否则如果is_function U ::value为真修改类型为add_pointer U ::type 否则修改类型为remove_cv U ::type
// 例
typedef std::decayint::type Normal; // int
typedef std::decayint::type Ref; // int
typedef std::decayint::type RefRef; // int
typedef std::decayconst int::type const; // int
typedef std::decayint[2]::type Array; // int*
typedef std::decayint(int)::type FunPtr; // int(*)(int) 函数指针因此利用std::decay可以方便的获得函数指针。
std::remove_cvconst int::type t; //t为int类型
t 0;
decltype(t) t2 1; //t2为int类型
3、查询判断 1实现基础为编译期的true和false
templateclass _Ty,_Ty _Valstruct integral_constant{ // convenient template for integral constant typesstatic constexpr _Ty value _Val;using value_type _Ty;using type integral_constant;constexpr operator value_type() const noexcept{ // return stored valuereturn (value);}_NODISCARD constexpr value_type operator()() const noexcept{ // return stored valuereturn (value);}};templatebool _Valusing bool_constant integral_constantbool, _Val;using true_type bool_constanttrue;
using false_type bool_constantfalse;2类型判断
is_void
is_enum
is_integral //int系列
is_floating_point //浮点数系列
is_pointer
is_null_pointer //C11引入的一种类型std::nullptr_t
is_arithmetic //算数类型。int系列、float系列
is_fundamental //int系列、float系列、void、nullptr_t
is_compound //化合物。例如自定义类型、指针。等价!is_function
is_scalar //C标准类型
is_union
is_class
is_array
is_object //不为函数、不为引用、不为void
is_function使用示例
bool b std::true_type::value;
b std::is_lvalue_referenceint ::value;3修饰的判断
is_const
is_volatile
is_lvalue_reference
is_rvalue_reference
is_reference4class的定制判断
is_polymorphic //含有虚函数表的类
is_abstract //抽象的不可实例化的类
is_final //禁止重写或继承
is_standard_layout //标准布局
is_trivial
is_trivially_copyable
is_empty //空类
is_constructible
is_destructible
is_member_function_pointer
is_member_object_pointer
is_copy_constructible
is_default_constructible
is_move_constructible
is_assignable
is_copy_assignable
is_move_assignable
has_virtual_destructor详细使用参考C冷知识二——类型判断之性能优化
5、类型推导等复杂计算
is_same //判断两种类型是否相同
is_convertible //判断两种类型是否可以隐式转换
conditional //根据一个判断式选择两个类型中的一个类似三元表达式
enable_if //判断一个判断式的结果是否为true
extent //计算数组第N(0开始默认值)维度元素的个数
rank //计算数组类型的维度
result_of //获取可调用对象返回值的类型使用示例
bool b std::is_sameint, bool::value;
b std::is_convertiblebool, int::value;
std::conditionaltrue, int, bool::type t 0; //t为int类型四、type_traits的高阶工具
以下用法需完全掌握
ref/cref //引用的封装类似智能指针。针对bind和thrad等导致引用失效
invoke //立即执行可调用对象
function //将一个可调用对象封装储存供后续调用
bind //通用函数适配器
forward //精准转发同类型文章参考Traits和Policy Classes
有错误或不足欢迎评论指出创作不易转载请注明出处。如有帮助记得点赞关注哦(⊙o⊙) 更多内容请关注个人博客https://blog.csdn.net/qq_43148810