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

南京浦口网站建设高端响应式网站

南京浦口网站建设,高端响应式网站,宁波四方网络网站建设,做网站建设的有哪些一、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.yayakq.cn/news/527184/

相关文章:

  • 高端网站设计报价二手车网站开发多少钱
  • 浏览器怎么设置不拦截任何网站企业名称怎么取
  • 仿163源码交易平台宽屏整站源码 网站模板交易平台源码如何速发布wordpress
  • 网站开发和浏览器兼容问题手机排行榜2021前十名性价比
  • 做网站不赚钱了网站开发基础与提高
  • 网站打开时的客户引导页品牌建设的最高境界是培育客户成为什么购买者
  • 网站开发设计总结及心得体会门户网站应该怎么做
  • 潮流资讯类网站建设策划网站做外链平台有哪些
  • 网站备案幕布尺寸服务器搭建网站用什么系统
  • 崂山网站建设友情链接是外链吗
  • 网站赚钱方法wordpress横向导航菜单主题
  • wordpress转移整站wordpress导航栏下拉菜单代码
  • dreamwearver怎么做静态网站网络策略
  • 手机网站开发要多久网页设计资料下载网站
  • 珠海网站建设那家好中国flash网站模板中心
  • 百度收录的网站已经失效网站收录量下降
  • 网站 编码文档想做个赚钱的网站不知道做那种
  • 网站布局分类上海南山做网站
  • 寻找南京帮助做网站的单位网站架构发展历程的思考和心得体会
  • 佛山网站建设公司哪家性价比高wordpress安装环境
  • 刘金鹏做网站下载爱南宁app下载
  • html做的网站排版导致乱码万网的app叫什么
  • 阿里接外包吗网站开发网站开发制作的流程是什么
  • 精美网站开发方案wordpress 翻页功能
  • 学校网站怎么做门户网站建设需要多少钱
  • 免费 网站 手机西安黑马程序员培训机构
  • 网站建设倒计时单页源码麦客网做网站
  • 做企业网站所需要的资料郑州建站优化
  • 怎么健免费网站建筑网片多少钱一平方
  • 注册网站模板宁波网站建设 熊掌号