当前位置: 首页 > news >正文

C++标准模板库type_traits源码剖析

一、type_traits源码介绍

1、type_traits是C++11提供的模板元基础库。
2、type_traits可实现在编译期计算。包括添加修饰、萃取、判断查询、类型推导等等功能。
3、type_traits提供了编译期的true和false。

二、type_traits的作用

1、根据不同类型,模板匹配不同版本的算法
STL中的Algorithm通过Iterator存取Container内容,Functor可以协助Algorithm完全不同的策略变化。此变化请参见:C++模板编程之类型萃取 惊鸿一瞥

2、编译检查模板类型复合预期
标准库经典示例:C++11标准库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_const<int>::type t = 0;	//t为const int类型
//std::add_const<int>::type t2;	//编译不通过,const定义时需要初始化

2、萃取

remove_const
remove_volatile
remove_cv
remove_reference			//退化左、右值引用
remove_pointer				
remove_extent				//数组类型,退化一个维度
remove_all_extents			//数组类型,退化所有维度
decay						//退化、衰弱复合运算;const、volatile、reference、extent
autodecltype				//根据值自动推导类型。

1)auto和decltype
详细用法参见:C++11新特性:auto和decltype

2)type_traits std::decay(朽化
对于普通类型移除引用和cv符(const和volatile),规则如下:
移除T类型的引用,得到类型U,U为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::decay<int>::type Normal;    // int
typedef std::decay<int&>::type Ref;    // int
typedef std::decay<int&&>::type RefRef;    // int
typedef std::decay<const int&>::type const;    // int
typedef std::decay<int[2]>::type Array;    // int*
typedef std::decay<int(int)>::type FunPtr;    // int(*)(int) 函数指针

因此,利用std::decay可以方便的获得函数指针。

std::remove_cv<const int>::type t;	//t为int类型
t = 0;
decltype(t) t2 = 1;					//t2为int类型

3、查询判断
1)实现基础为编译期的true和false:

template<class _Ty,_Ty _Val>struct 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);}};template<bool _Val>using bool_constant = integral_constant<bool, _Val>;using true_type = bool_constant<true>;
using false_type = bool_constant<false>;

2)类型判断

is_void
is_enum
is_integral					//int系列
is_floating_point			//浮点数系列
is_pointer
is_null_pointer				//C++11引入的一种类型,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_reference<int> ::value;

3)修饰的判断

is_const
is_volatile
is_lvalue_reference
is_rvalue_reference
is_reference

4)class的定制判断

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_same<int, bool>::value;
b = std::is_convertible<bool, int>::value;
std::conditional<true, 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

http://www.lryc.cn/news/1599.html

相关文章:

  • Python获取公众号(pc客户端)数据,使用Fiddler抓包工具
  • Maven进阶
  • AXI实战(一)-为AXI总线搭建简单的仿真测试环境
  • 数据库管理-第五十六期 监控(20230210)
  • 测试开发,测试架构师为什么能拿50 60k呢需要掌握哪些技能呢
  • Miniblink 入门
  • [python入门㊷] - python存储数据
  • Little Fighter:旺角——NFT 系列来袭!
  • 基础篇:01-微服务概述
  • TC358775XBG替代方案|完美替代 TC358775XBG替代方案|低BOM成本DSI转LVDS方案CS5518
  • Android开发
  • virtualbox虚拟机导入到vmware esxi虚拟机
  • 如何使用命名空间管理C++代码
  • 海思3559:BT656调试笔记
  • reactor之hooks
  • 单片AR眼镜Monocle揭秘:基于反射棱镜,重15g续航1小时
  • 计算机视觉框架OpenMMLab开源学习(五):目标检测实战
  • SpringIOC推导IOC初步
  • Linux(centOS7)虚拟机中配置 vim
  • 消息中间件-RocketMQ入门 消息发送的三种方式
  • 【FLASH存储器系列十九】固态硬盘掉电后如何恢复掉电前状态?
  • Java知识点细节简易汇总——(7)面向对象编程(高级部分)
  • 阻塞式队列-生产者消费者模型
  • 引导滤波code
  • Leetcode.2353 设计食物评分系统
  • C语言学习_DAY_2_变量的定义_输入与输出
  • mac 安装navicat
  • RocketMQ快速入门
  • 【虚拟仿真】Unity3D实现从浏览器拉起本地exe程序并传参数
  • Intel中断体系(1)中断与异常处理