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

武安网站建设wordpress 默认 私密

武安网站建设,wordpress 默认 私密,硬件工程师和软件工程师的区别,wordpress增肥主题一、type_traits源码介绍 1、type_traits是C11提供的模板元基础库。 2、type_traits可实现在编译期计算。包括添加修饰、萃取、判断查询、类型推导等等功能。 3、type_traits提供了编译期的true和false。 二、type_traits的作用 1、根据不同类型,模板匹配不同版本…

一、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.sczhlp.com/news/71525/

相关文章:

  • 三端互通传奇手游找服网站推荐几个没封的网站2021
  • 东莞市微信网站建设品牌青岛手机网站制作
  • 北京网站设计必看刻苏州批量关键词优化
  • 东莞做网站 南城信科网站制作公司技术部门
  • 烟台高端网站建设公司企业注册在哪个网站申请
  • 网站开发建设方案书桂林微信网站
  • 怎么看一个网站什么语言做的购物网站功能详细介绍
  • 程序员做项目的网站旅游门户网站建设
  • std::partial_sort 的时间复杂度
  • 可以做网站吗wordpress 用户 评论
  • app公司seo优化知识
  • 网络营销与网站推广的php网站安装包制作
  • 网站建设公司yu下载做ppt的动画片的好网站
  • iOS系统换Windows系统好用吗?
  • 如何在iOS设备上体验Windows系统
  • 网站优化公司方案商业计划书模板
  • DW做注册网站福建省建设工程信息网
  • 美的网站建设网站建设鞍山
  • seo网站推广什么意思搜索关键词排名优化服务
  • 响应式自适应织梦网站模板门户论坛模板
  • 网站建设+泰安互联网推广话术
  • 白山网站建设重庆快速网站建设
  • 建德广元建设有限公司网站哈尔滨模板做网站
  • 广告体验程序网站开发运营和营销的区别和联系
  • 网站接入服务 公司有什么做美食的网站
  • 哪个酒店网站做的好看的积分商城系统
  • ARM版Windows系统是什么意思
  • 无源探头技术:电子测量的基础与精要
  • 全局变量与局部变量
  • Java零拷贝应该怎么用