溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++可變參數(shù)模板的展開方式是什么

發(fā)布時(shí)間:2022-04-06 15:16:11 來源:億速云 閱讀:149 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“C++可變參數(shù)模板的展開方式是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C++可變參數(shù)模板的展開方式是什么”吧!

可變參數(shù)模板(variadic templates)是C++11新增的強(qiáng)大的特性之一,它對(duì)模板參數(shù)進(jìn)行了高度泛化,能表示0到任意個(gè)數(shù)、任意類型的參數(shù)。相比C++98/03這些類模版和函數(shù)模版中只能含固定數(shù)量模版參數(shù)的“老古董”,可變模版參數(shù)無疑是一個(gè)巨大的進(jìn)步。

如果是剛接觸可變參數(shù)模板可能會(huì)覺得比較抽象,使用起來會(huì)不太順手,使用可變參數(shù)模板時(shí)通常離不開模板參數(shù)的展開,所以本文來列舉一些常用的模板展開方式,幫助我們來對(duì)可變參數(shù)模板有一個(gè)初步的了解。

可變參數(shù)模板的定義

可變參數(shù)模板和普通模板的定義類似,在寫法上需要在 typenameclass 后面帶上省略號(hào)...,以下為一個(gè)常見的可變參數(shù)函數(shù)模板:

template <class... T>void func(T... args){    //...}

上面這個(gè)函數(shù)模板的參數(shù) args 前面有省略號(hào),所以它就是一個(gè)被稱為模板參數(shù)包(template parameter pack)的可變模版參數(shù),它里面包含了0到N個(gè)模版參數(shù),而我們是無法直接獲取 args 中的每個(gè)參數(shù)的,只能通過展開參數(shù)包的方式來獲取參數(shù)包中的每個(gè)參數(shù),這也是本文要重點(diǎn)總結(jié)的內(nèi)容。

參數(shù)包的展開

參數(shù)包展開的方式隨著c++語言的發(fā)展也在與時(shí)俱進(jìn),我們以實(shí)現(xiàn)一個(gè)可變參格式化打印函數(shù)為例,列舉一些常用的方式:

遞歸函數(shù)方式展開

#include <iostream>void FormatPrint(){    std::cout << std::endl;}template <class T, class ...Args>void FormatPrint(T first, Args... args){   std::cout << "[" << first << "]";   FormatPrint(args...);}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

這種遞歸展開的方式與遞歸函數(shù)的定義是一樣的,需要遞歸出口和不斷調(diào)用自身,仔細(xì)看看這個(gè)函數(shù)模板是不是都滿足啦?遞歸出口就是這個(gè)無模板參數(shù)的 FormatPrint,并且在有參模板中一直在調(diào)用自身,遞歸調(diào)用的過程時(shí)這樣的 FormatPrint(4,3,2,1) -> FormatPrint(3,2,1) -> FormatPrint(2,1) -> FormatPrint(1) -> FormatPrint(),輸出內(nèi)容如下:

>albert@home-pc:/mnt/d/data/cpp/testtemplate$ g++ testtemplate.cpp --std=c++11albert@home-pc:/mnt/d/data/cpp/testtemplate$ ./a.out[1][2][3][4][good][2][hello][4][110]

逗號(hào)表達(dá)式展開

#include <iostream>template <class ...Args>void FormatPrint(Args... args){   (void)std::initializer_list<int>{ (std::cout << "[" << args << "]", 0)... };   std::cout << std::endl;}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

這種方式用到了C++11的新特性初始化列表(Initializer lists)以及很傳統(tǒng)的逗號(hào)表達(dá)式,我們知道逗號(hào)表達(dá)式的優(yōu)先級(jí)最低,(a, b) 這個(gè)表達(dá)式的值就是 b,那么上述代碼中(std::cout << "[" << args << "]", 0)這個(gè)表達(dá)式的值就是0,初始化列表保證其中的內(nèi)容從左往右執(zhí)行,args參數(shù)包會(huì)被逐步展開,表達(dá)式前的(void)是為了防止變量未使用的警告,運(yùn)行過后我們就得到了一個(gè)N個(gè)元素為0的初始化列表,內(nèi)容也被格式化輸出了:

albert@home-pc:/mnt/d/data/cpp/testtemplate$ g++ testtemplate.cpp --std=c++11albert@home-pc:/mnt/d/data/cpp/testtemplate$ ./a.out[1][2][3][4][good][2][hello][4][110]

說到這順便提一下,可以使用sizeof...(args)得到參數(shù)包中參數(shù)個(gè)數(shù)。

enable_if方式展開

#include <iostream>#include <tuple>#include <type_traits>template<std::size_t k = 0, typename tup>typename std::enable_if<k == std::tuple_size<tup>::value>::type FormatTuple(const tup& t){    std::cout << std::endl;}template<std::size_t k = 0, typename tup>typename std::enable_if<k < std::tuple_size<tup>::value>::type FormatTuple(const tup& t){    std::cout << "[" << std::get<k>(t) << "]";    FormatTuple<k + 1>(t);}template<typename... Args>void FormatPrint(Args... args){    FormatTuple(std::make_tuple(args...));}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

C++11的enable_if常用于構(gòu)建需要根據(jù)不同的類型的條件實(shí)例化不同模板的時(shí)候。顧名思義,當(dāng)滿足條件時(shí)類型有效。可作為選擇類型的小工具,其廣泛的應(yīng)用在 C++ 的模板元編程(meta programming)之中,利用的就是SFINAE原則,英文全稱為Substitution failure is not an error,意思就是匹配失敗不是錯(cuò)誤,假如有一個(gè)特化會(huì)導(dǎo)致編譯時(shí)錯(cuò)誤,只要還有別的選擇,那么就無視這個(gè)特化錯(cuò)誤而去選擇另外的實(shí)現(xiàn),這里的特化概念不再展開,感興趣可以自行了解,后續(xù)可以單獨(dú)總結(jié)一下。

在上面的代碼實(shí)現(xiàn)中,基本思路是先將可變模版參數(shù)轉(zhuǎn)換為std::tuple,然后通過遞增參數(shù)的索引來選擇恰當(dāng)?shù)?code>FormatTuple函數(shù),當(dāng)參數(shù)的索引小于tuple元素個(gè)數(shù)時(shí),會(huì)不斷取出當(dāng)前索引位置的參數(shù)并輸出,當(dāng)參數(shù)索引等于總的參數(shù)個(gè)數(shù)時(shí)調(diào)用另一個(gè)模板重載函數(shù)終止遞歸,編譯運(yùn)行輸入以下內(nèi)容:

albert@home-pc:/mnt/d/data/cpp/testtemplate$ g++ testtemplate.cpp --std=c++11albert@home-pc:/mnt/d/data/cpp/testtemplate$ ./a.out[1][2][3][4][good][2][hello][4][110]

折疊表達(dá)式展開(c++17)

#include <iostream>template<typename... Args>void FormatPrint(Args... args){    (std::cout << ... << args) << std::endl;}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

折疊表達(dá)式(Fold Expressions)是C++17新引進(jìn)的語法特性,使用折疊表達(dá)式可以簡(jiǎn)化對(duì)C++11中引入的參數(shù)包的處理,可以在某些情況下避免使用遞歸,更加方便的展開參數(shù),如上述代碼中展示的這樣可以方便的展開參數(shù)包,不過輸出的內(nèi)容和之前的有些不一樣:

albert@home-pc:/mnt/d/data/cpp/testtemplate$ g++ testtemplate.cpp --std=c++17albert@home-pc:/mnt/d/data/cpp/testtemplate$ ./a.out1234good2hello4110

對(duì)比結(jié)果發(fā)現(xiàn)缺少了格式化的信息,需要以輔助函數(shù)的方式來格式化:

#include <iostream>template<typename T>string format(T t) {    std::stringstream ss;    ss << "[" << t << "]";    return ss.str();}template<typename... Args>void FormatPrint(Args... args){    (std::cout << ... << format(args)) << std::endl;}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

這次格式化內(nèi)容就被加進(jìn)來了:

albert@home-pc:/mnt/d/data/cpp/testtemplate$ g++ testtemplate.cpp --std=c++17albert@home-pc:/mnt/d/data/cpp/testtemplate$ ./a.out[1][2][3][4][good][2][hello][4][110]

這樣好像還是有點(diǎn)麻煩,我們可以把折疊表達(dá)式和逗號(hào)表達(dá)式組合使用,這樣得到的代碼就簡(jiǎn)單多啦,也能完成格式化輸出的任務(wù):

#include <iostream>template<typename... Args>void FormatPrint(Args... args){    (std::cout << ... << (std::cout << "[" << args, "]")) << std::endl;}int main(void){   FormatPrint(1, 2, 3, 4);   FormatPrint("good", 2, "hello", 4, 110);   return 0;}

感謝各位的閱讀,以上就是“C++可變參數(shù)模板的展開方式是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C++可變參數(shù)模板的展開方式是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI