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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可变参数模板

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可变参数模板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【導讀】:C++ 可變參數模板對參數進行了高度泛化,它能表示0到任意個數、任意類型的參數。相比C++98/03,類模版和函數模版中只能含固定數量的模版參數,可變模版參數無疑是一個巨大的改進。然而由于可變模版參數比較抽象,使用起來需要一定的技巧,所以它也是C++11中最難理解和掌握的特性之一。那么請大家跟隨小編,一起來學習吧。

?

以下是正文


?

?

概述

?

在C++11之前,類模板和函數模板只能含有固定數量的模板參數。C++11增強了模板功能,允許模板定義中包含0到任意個模板參數,這就是可變參數模板。

?

可變參數模板和普通模板的語義是一樣的,只是寫法上稍有區別,聲明可變參數模板時需要在typename或class后面帶上省略號“…”:

?

//T叫模板參數包,args叫函數參數包template<class ... T> void func(T ... args){//可變參數模板函數}func(); // OK:args不含有任何實參func(1); // OK:args含有一個實參:intfunc(2, 1.0); // OK:args含有兩個實參int和double

T叫模板參數包,args叫函數參數包。

?

省略號“…”的作用有兩個:

?

(1)聲明一個參數包,這個參數包中可以包含0到任意個模板參數

?

(2)在模板定義的右邊,可以將參數包展開成一個一個獨立的參數

?

可變參數模板函數

?

可變參數模板函數的定義

?

一個可變參數模板函數的定義如下:

?

#include <iostream>using namespace std;template<class ... T> void func(T ... args){//可變參數模板函數 //sizeof...(sizeof后面有3個小點)計算變參個數 cout << "num = " << sizeof...(args) << endl;}int main(){ func(); // num = 0 func(1); // num = 1 func(2, 1.0); // num = 2 return 0;}

?

運行結果如下:
參數包的展開

?

遞歸方式展開

?

通過遞歸函數展開參數包,需要提供一個參數包展開的函數和一個遞歸終止函數。

?

#include <iostream>using namespace std;//遞歸終止函數void debug(){ cout << "empty\n";}//展開函數template <class T, class ... Args>void debug(T first, Args ... last){ cout << "parameter " << first << endl; debug(last...);}int main(){ debug(1, 2, 3, 4); return 0;}

?

運行結果如下:

遞歸調用過程如下:

?

debug(1, 2, 3, 4);debug(2, 3, 4);debug(3, 4);debug(4);debug();

?

通過可變參數模板實現打印函數:

?

#include <iostream>#include <stdexcept>using namespace std;void Debug(const char* s){ while (*s) { if (*s == '%' && *++s != '%') { throw runtime_error("invalid format string: missing arguments"); } cout << *s++; }}template<typename T, typename... Args>void Debug(const char* s, T value, Args... args){ while (*s) { if (*s == '%' && *++s != '%') { cout << value; return Debug(++s, args...); } cout << *s++; } throw runtime_error("extra arguments provided to Debug");} int main(){ Debug("a = %d, b = %c, c = %s\n", 250, 'm', "mike"); return 0;}

?

運行結果如下:

非遞歸方式展開

?

#include <iostream>using namespace std;template <class T>void print(T arg){ cout << arg << endl;}template <class ... Args>void expand(Args ... args){ int a[] = { (print(args), 0)... };}int main(){ expand(1, 2, 3, 4); return 0;}

運行結果如下:

expand函數的逗號表達式:(print(args), 0), 也是按照這個執行順序,先執行print(args),再得到逗號表達式的結果0。

?

同時,通過初始化列表來初始化一個變長數組,{ (print(args), 0)… }將會展開成( (print(args1), 0), (print(args2), 0), (print(args3), 0), etc…), 最終會創建一個元素只都為0的數組int a[sizeof…(args)]。

?

可變參數模板類

?

繼承方式展開參數包

?

可變參數模板類的展開一般需要定義2 ~ 3個類,包含類聲明和特化的模板類:

???????

#include <iostream>#include <typeinfo>using namespace std;template<typename... A> class BMW{}; // 變長模板的聲明template<typename Head, typename... Tail> // 遞歸的偏特化定義class BMW<Head, Tail...> : public BMW<Tail...>{//當實例化對象時,則會引起基類的遞歸構造public: BMW() { printf("type: %s\n", typeid(Head).name()); } Head head;};template<> class BMW<>{}; // 邊界條件int main(){ BMW<int, char, float> car; return 0;}

?

運行結果如下:

模板遞歸和特化方式展開參數包

???????

#include <iostream>using namespace std;template <long... nums> struct Multiply;// 變長模板的聲明template <long first, long... last>struct Multiply<first, last...> // 變長模板類{ static const long val = first * Multiply<last...>::val;};template<>struct Multiply<> // 邊界條件{ static const long val = 1;};int main(){ cout << Multiply<2, 3, 4, 5>::val << endl; // 120 return 0;}

?

運行結果如下:

- EOF -

總結

以上是生活随笔為你收集整理的可变参数模板的全部內容,希望文章能夠幫你解決所遇到的問題。

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