臺(tái)山網(wǎng)站建設(shè)口碑營(yíng)銷的例子
一、type_traits源碼介紹
1、type_traits是C++11提供的模板元基礎(chǔ)庫(kù)。
2、type_traits可實(shí)現(xiàn)在編譯期計(jì)算。包括添加修飾、萃取、判斷查詢、類型推導(dǎo)等等功能。
3、type_traits提供了編譯期的true和false。
二、type_traits的作用
1、根據(jù)不同類型,模板匹配不同版本的算法
STL中的Algorithm通過(guò)Iterator存取Container內(nèi)容,Functor可以協(xié)助Algorithm完全不同的策略變化。此變化請(qǐng)參見(jiàn):C++模板編程之類型萃取 驚鴻一瞥
2、編譯檢查模板類型復(fù)合預(yù)期
標(biāo)準(zhǔn)庫(kù)經(jīng)典示例:C++11標(biāo)準(zhǔn)庫(kù)thread構(gòu)造函數(shù)淺析
三、type_traits源碼剖析
1、添加修飾
std::add_const
std::add_volatile
std::add_cv //同時(shí)添加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; //編譯不通過(guò),const定義時(shí)需要初始化
2、萃取
remove_const
remove_volatile
remove_cv
remove_reference //退化左、右值引用
remove_pointer
remove_extent //數(shù)組類型,退化一個(gè)維度
remove_all_extents //數(shù)組類型,退化所有維度
decay //退化、衰弱復(fù)合運(yùn)算;const、volatile、reference、extent
auto和decltype //根據(jù)值自動(dòng)推導(dǎo)類型。
1)auto和decltype
詳細(xì)用法參見(jiàn):C++11新特性:auto和decltype
2)type_traits std::decay(朽化
對(duì)于普通類型移除引用和cv符(const和volatile),規(guī)則如下:
移除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) 函數(shù)指針
因此,利用std::decay可以方便的獲得函數(shù)指針。
std::remove_cv<const int>::type t; //t為int類型
t = 0;
decltype(t) t2 = 1; //t2為int類型
3、查詢判斷
1)實(shí)現(xiàn)基礎(chǔ)為編譯期的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 //浮點(diǎn)數(shù)系列
is_pointer
is_null_pointer //C++11引入的一種類型,std::nullptr_t
is_arithmetic //算數(shù)類型。int系列、float系列
is_fundamental //int系列、float系列、void、nullptr_t
is_compound //化合物。例如:自定義類型、指針。等價(jià)!is_function
is_scalar //C++標(biāo)準(zhǔn)類型
is_union
is_class
is_array
is_object //不為函數(shù)、不為引用、不為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 //含有虛函數(shù)表的類
is_abstract //抽象的,不可實(shí)例化的類
is_final //禁止重寫或繼承
is_standard_layout //標(biāo)準(zhǔn)布局
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
詳細(xì)使用參考:C++冷知識(shí)(二)——類型判斷之性能優(yōu)化
5、類型推導(dǎo)等復(fù)雜計(jì)算
is_same //判斷兩種類型是否相同
is_convertible //判斷兩種類型是否可以隱式轉(zhuǎn)換
conditional //根據(jù)一個(gè)判斷式選擇兩個(gè)類型中的一個(gè),類似三元表達(dá)式
enable_if //判斷一個(gè)判斷式的結(jié)果是否為true
extent //計(jì)算數(shù)組第N(0開(kāi)始,默認(rèn)值)維度元素的個(gè)數(shù)
rank //計(jì)算數(shù)組類型的維度
result_of //獲取可調(diào)用對(duì)象返回值的類型
使用示例:
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 //引用的封裝,類似智能指針。針對(duì)bind和thrad等導(dǎo)致引用失效
invoke //立即執(zhí)行可調(diào)用對(duì)象
function //將一個(gè)可調(diào)用對(duì)象封裝儲(chǔ)存,供后續(xù)調(diào)用
bind //通用函數(shù)適配器
forward //精準(zhǔn)轉(zhuǎn)發(fā)
同類型文章參考:Traits和Policy Classes
有錯(cuò)誤或不足歡迎評(píng)論指出!創(chuàng)作不易,轉(zhuǎn)載請(qǐng)注明出處。如有幫助,記得點(diǎn)贊關(guān)注哦(⊙o⊙)
更多內(nèi)容請(qǐng)關(guān)注個(gè)人博客:https://blog.csdn.net/qq_43148810