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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ template(9)trait和Policy

發布時間:2024/4/11 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ template(9)trait和Policy 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先看一個Demo:

累加序列:

template <typename T> inline T accum (T const* beg, T const* end) { T total = T(); // assume T() actually creates a zero value while (beg != end) { total += *beg; ++beg; } return total; }

使用示例:

int main() {// create array of 5 integer values int num[]={1,2,3,4,5}; // print average value std::cout << "the average value of the integer values is " << accum(&num[0], &num[5]) / 5 << '\n'; // create array of character values char name[] = "templates"; int length = sizeof(name)-1; // (try to) print average character value std::cout << "the average value of the characters in \"" << name << "\" is " << accum(&name[0], &name[length]) / length << '\n'; return 1;}

結果:

顯然char的類型有問題,變量total 應該轉化為int

修改方案則是新加一個模版參數:

template <typename T,typename V=int> inline V accum (T const* beg, T const* end) { V total = V(); // assume T() actually creates a zero value while (beg != end) { total += *beg; ++beg; } return total; }

同時添加了使用成本

accum<char,int>(&name[0], &name[length])

模版的trait力量:將動態模版參數定義到外部結構中

template<typename T> class AccumulationTraits; template<> class AccumulationTraits<char> { public: typedef int AccT; }; template<> class AccumulationTraits<short> { public: typedef int AccT; }; template<> class AccumulationTraits<int> { public: typedef long AccT; }; template<> class AccumulationTraits<unsigned int> { public: typedef unsigned long AccT; }; template<> class AccumulationTraits<float> { public: typedef double AccT; }; template <typename T> inline typename AccumulationTraits<T>::AccT accum (T const* beg, T const* end) { // return type is traits of the element type typedef typename AccumulationTraits<T>::AccT AccT; AccT total = AccT(); // assume T() actually creates a zero value while (beg != end) { total += *beg; ++beg; } return total; }

現在可以保持調用方式保持不變,通過trait來更改類型參數

accum(&name[0], &name[length])

現在結果是正確的

trait的默認值

trait類型的初始值用構造函數初始化可能會有問題,所以可以再次用trait來定義默認值

默認值方式:1.靜態變量(只能整型數值和枚舉),2.靜態方法(推薦)

template<> class AccumulationTraits<char> { public: typedef int AccT; //static AccT const zero = 0; static AccT zero() { return 0; } };

更改后的初始化方式:

template <typename T> inline typename AccumulationTraits<T>::AccT accum (T const* beg, T const* end) { // return type is traits of the element type typedef typename AccumulationTraits<T>::AccT AccT; AccT total = AccumulationTraits<T>::zero();while (beg != end) { total += *beg; ++beg; } return total; }

This is the key of the traits concept: Traits provide an avenue to configure concrete elements (mostly types) for generic computations.

trait參數化

有時候我們想改變通過trait本身的默認模版參數,這樣就需要對trait本事做一個參數封裝

封裝一個默認的模版參數

template <typename T, typename AT = AccumulationTraits<T> > class Accum { public: static typename AT::AccT accum (T const* beg, T const* end) { typename AT::AccT total = AT::zero(); while (beg != end) { total += *beg; ++beg; } return total; } };

1

現在之前的方法應該如下示例:

template <typename T> inline typename AccumulationTraits<T>::AccT accum (T const* beg, T const* end) { return Accum<T>::accum(beg, end); }

trait參數化

template <typename Traits, typename T> inline typename Traits::AccT accum (T const* beg, T const* end) { return Accum<T, Traits>::accum(beg, end); }

?

Policy

Policy注重算法,如下示例:Policy是一個靜態類,通過動態編譯來計算

template <typename T, typename Policy = SumPolicy, typename Traits = AccumulationTraits<T> > class Accum { public: typedef typename Traits::AccT AccT; static AccT accum (T const* beg, T const* end) { AccT total = Traits::zero(); while (beg != end) { Policy::accumulate(total, *beg); ++beg; } return total; } };

不同Policy算法

class SumPolicy { public: template<typename T1, typename T2> static void accumulate (T1& total, T const & value) { total += value; } }; class MultPolicy { public: template<typename T1, typename T2> static void accumulate (T1& total, T const& value) { total *= value; } };

使用示例:

int main() { // create array of 5 integer values int num[]={1,2,3,4,5}; // print product of all values std::cout << "the product of the integer values is " << Accum<int,MultPolicy>::accum(&num[0], &num[5]) << '\n'; }

總結

以上是生活随笔為你收集整理的c++ template(9)trait和Policy的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲一区自拍 | 成年视频在线播放 | 中文字幕在线观看 | 久久久久久国产免费a片 | 韩日三级视频 | 久久久久91| 日韩成人小视频 | 色婷婷六月 | 中文字幕国产综合 | 97人人爽人人 | 黄色激情在线观看 | 欧美性受xxxx黑人xyx性 | 综合一区在线 | 无码人妻aⅴ一区二区三区玉蒲团 | 秋霞欧美在线观看 | 日本大尺度电影免费观看全集中文版 | 亚洲欧美综合网 | 国产对白在线 | 超碰人人网| 亚州中文 | 亚洲国产精品女人久久久 | 美女视频一区二区 | 日本一区二区三区中文字幕 | 精品一区二区三区久久 | 牛牛超碰 | 乱人伦中文字幕 | 男人的天堂色偷偷 | www.超碰在线观看 | 黄色网址在线看 | 欧洲熟妇的性久久久久久 | 亚洲高清中文字幕 | 国内av| 午夜精品极品粉嫩国产尤物 | 免费成人深夜 | 国产aⅴ无码片毛片一级一区2 | 久久久久亚洲av成人无码电影 | 成人午夜视频免费观看 | 精品一区二区三区免费看 | 91蝌蚪视频在线观看 | 国产在线xxx | 99在线视频播放 | 樱花av在线 | 日本免费在线一区 | 豆国产97在线 | 亚洲 | 99久久久久 | 99情趣网 | 日本精品国产 | 欧美日韩亚 | 色无极亚洲影院 | 爱视频福利网 | 在线观看av网 | 热久久久久久久 | 欧美中文字幕 | 国产黄色录相 | 刘亦菲国产毛片bd | 成人激情免费视频 | 日本午夜免费福利视频 | 成人在线一区二区 | 日日操日日干 | 人人爱国产 | 亚洲视频1区 | 男女网站免费 | 色婷婷激情网 | 91黄色影视 | 波多野在线视频 | 免费黄色在线 | 亚洲欧美自偷自拍 | 日本免费一区二区三区最新 | 亚洲图片视频在线 | 亚洲激情综合 | 99视频在线观看视频 | wwwxx国产 | 欧美大色网 | 视频1区| 日韩毛片免费看 | 四川操bbb | 天堂在线| 中文字幕国产精品 | 正在播放欧美 | 国产精品成 | 在线看视频 | 五月天婷婷网站 | 爱爱综合| 免费国产| 久久久久人妻一区 | 国产一区精品在线观看 | 国产又粗又猛又黄 | 精品欧美一区二区久久久久 | 公侵犯一区二区三区四区中文字幕 | 91精品人妻一区二区 | 中文字幕在线网址 | 91免费官网| 日韩在线观看免费av | 免费午夜影院 | 91免费黄视频 | www.youjizz国产 | 麻豆网站在线播放 | 欧美色图17p | av在线专区 |