日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C/Cpp / STL / 类型萃取

發(fā)布時間:2024/10/14 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/Cpp / STL / 类型萃取 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

作用

類型萃取使用模板技術(shù)來萃取類型(包含自定義類型和內(nèi)置類型)的某些特性,用以判斷該類型是否含有某些特性,從而在泛型算法中來對該類型進(jìn)行特殊的處理用來達(dá)到提高效率或者其他的目的。

類型萃取的實現(xiàn)的基石是模板的偏特化和全特化,詳解鏈接:https://blog.csdn.net/itworld123/article/details/104718336?。

實例說明

這里采用的實例是 STL 的 destroy() 函數(shù)的實現(xiàn),原型如下圖所示:(stl_contruct.h)

該函數(shù)的作用是析構(gòu)?[ first , last ) 范圍內(nèi)的對象。

這里面就有個問題,ForwardIterator 是否是類對象,如果是的話就可以執(zhí)行其析構(gòu)函數(shù),否則是不進(jìn)行任何處理的。現(xiàn)在的關(guān)鍵就是怎么才知道?ForwardIterator?的數(shù)據(jù)類型是屬于哪一類呢?

為了解決上述問題,這里面就需要使用類型萃取技術(shù)了。

首先我們通過 value_type(first) 獲取到了模板的數(shù)據(jù)類型。進(jìn)入 __destroy() 函數(shù),如下所示:(stl_contruct.h)

好的,關(guān)鍵時刻來了!trivial_destructor 就決定了類型 T 是否含有析構(gòu)函數(shù)!它是如何被聲明的呢?這里需要看下?__type_traits<T> 的代碼,如下所示:(type_traits.h)

struct __true_type { };struct __false_type { };template <class type> struct __type_traits {typedef __false_type has_trivial_default_constructor;typedef __false_type has_trivial_copy_constructor;typedef __false_type has_trivial_assignment_operator;typedef __false_type has_trivial_destructor;typedef __false_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<char> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<signed char> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<unsigned char> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<short> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<unsigned short> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<int> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<unsigned int> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<long> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<unsigned long> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<float> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<double> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };__STL_TEMPLATE_NULL struct __type_traits<long double> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };template <class T> struct __type_traits<T*> {typedef __true_type has_trivial_default_constructor;typedef __true_type has_trivial_copy_constructor;typedef __true_type has_trivial_assignment_operator;typedef __true_type has_trivial_destructor;typedef __true_type is_POD_type; };

上述代碼中充斥著?__type_traits 的原生版本、偏特化版本以及全特化版本,也就是說,STL采用窮舉的方案,解決了如何判斷各種數(shù)據(jù)類型是否有析構(gòu)函數(shù)的問題。

若?has_trivial_destructor =?__true_type,則類型 T 不是類類型,所以不需要進(jìn)行析構(gòu),執(zhí)行代碼如下:(stl_contruct.h)

若?has_trivial_destructor =?__false_type,則類型 T 是類類型,所以需要進(jìn)行析構(gòu),執(zhí)行代碼如下:(stl_contruct.h)

這樣就完成了 destroy() 函數(shù)的功能。

?

參考:https://blog.csdn.net/dawn_sf/article/details/70038126

(SAW:Game Over!)

總結(jié)

以上是生活随笔為你收集整理的C/Cpp / STL / 类型萃取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。