日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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