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构造函数浅析

3、根据呼叫端传入的变量或类型,萃取或推到新的类型或值,以适配后续算法
示例:

#include <iostream>
#include <type_traits>

template <typename T>
void fun(T a){
    if(std::is_same<T,int>::value){
        std::cout<<"T type is int"<<std::endl;
        //we can do something
    }else{
        std::cout<<"T type is not int"<<std::endl;
        //we can do other thing
    }
}

三、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 types
	static constexpr _Ty value = _Val;

	using value_type = _Ty;
	using type = integral_constant;

	constexpr operator value_type() const noexcept
		{	// return stored value
		return (value);
		}

	_NODISCARD constexpr value_type operator()() const noexcept
		{	// return stored value
		return (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							//精准转发

参考博文:
c++ 中ref关键字的应用
C++11标准库thread简介–>5.线程传参args
C++可调用对象
C++17 std::invoke
深入理解C++中的move和forward!

同类型文章参考:Traits和Policy Classes

有错误或不足欢迎评论指出!创作不易,转载请注明出处。如有帮助,记得点赞关注哦(⊙o⊙)
更多内容请关注个人博客:https://blog.csdn.net/qq_43148810

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大胡子的艾娃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值