C++函數(shù)模板與重載解析超詳細(xì)講解
1.快速上手
函數(shù)模板是通用的函數(shù)描述,也就是說(shuō),它們使用泛型來(lái)定義函數(shù)。
#include<iostream> using namespace std; template <typename T> void Swap(T &a,T &b);//模板原型 struct apple{ string name; double weight; int group; }; void show(apple x); int main(){ int a,b; a=1; b=2; Swap(a,b); cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; apple c={"Alice",200,1}; apple d={"Bob",250,2}; Swap(c,d); cout<<"c:"<<endl; show(c); cout<<"d:"<<endl; show(d); } template <typename T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; } void show(apple x){ cout<<"name:"<<x.name<<endl; cout<<"weight:"<<x.weight<<endl; cout<<"group:"<<x.group<<endl; }
a:2
b:1
c:
name:Bob
weight:250
group:2
d:
name:Alice
weight:200
group:1
模板函數(shù)也可以有原型:
template <typename T>
void Swap(T &a,T &b);
這里的typename
也可以換成class
。
不過(guò)模板原型實(shí)際上不常見(jiàn)。
模板函數(shù)定義:
template <typename T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; }
模板函數(shù)隱式實(shí)例化:
Swap(a,b);
模板函數(shù)會(huì)根據(jù)實(shí)參的類型,給出函數(shù)定義。 還有顯式實(shí)例化: Swap<int>(a,b);
顯式的定義typename。 對(duì)于這兩種實(shí)例化,我推薦使用顯式實(shí)例化,因?yàn)殡[式實(shí)例化容易出錯(cuò)。對(duì)于這塊知識(shí)的詳細(xì)解讀,需要有對(duì)編譯器有充分的理解,在文章后面會(huì)給出。
一般我們不會(huì)用到模板函數(shù)的原型,因?yàn)槲覀円话惆涯0搴瘮?shù)的定義放在頭文件里面,再需要使用的時(shí)候,包含頭文件就行了。
不推薦的做法:模板原型放在頭文件,模板定義放在cpp文件里。
2.重載的模板
如果對(duì)函數(shù)的重載不了解,可以翻看我之前的文章:
內(nèi)聯(lián)函數(shù)、引用變量、函數(shù)重載
模板函數(shù)也可以重載,語(yǔ)法和常規(guī)函數(shù)的重載差不多;被重載的模板函數(shù)必須要特征標(biāo)不同。
#include<iostream> using namespace std; template <typename T> void Swap(T &a,T &b);//模板原型 template <typename T> void Swap(T *a,T *b,int n);//模板原型 struct apple{ string name; double weight; int group; }; void show(apple x); int main(){ int a,b; a=1; b=2; Swap(a,b); cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; apple c={"Alice",200,1}; apple d={"Bob",250,2}; Swap(c,d); cout<<"c:"<<endl; show(c); cout<<"d:"<<endl; show(d); char e[10]="hello"; char f[10]="bye!!"; Swap(e,f,10); cout<<"e:"<<e<<endl; cout<<"f:"<<f<<endl; } template <typename T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; } template <typename T> void Swap(T *a,T *b,int n){ T temp; for(int i=0;i<n;i++){ temp=a[i]; a[i]=b[i]; b[i]=temp; } } void show(apple x){ cout<<"name:"<<x.name<<endl; cout<<"weight:"<<x.weight<<endl; cout<<"group:"<<x.group<<endl; }
a:2
b:1
c:
name:Bob
weight:250
group:2
d:
name:Alice
weight:200
group:1
e:bye!!
f:hello
3.模板的局限性
#include<iostream> using namespace std; template<class T> const T& foo(const T &a,const T &b){ if(a>b)return a; else return b; } struct apple{ string name; double weight; int group; }; void show(apple x); int main(){ apple c={"Alice",200,1}; apple d={"Bob",250,2}; apple max=foo(c,d); show(max); } void show(apple x){ cout<<"name:"<<x.name<<endl; cout<<"weight:"<<x.weight<<endl; cout<<"group:"<<x.group<<endl; }
上面這段代碼是出錯(cuò)的,因?yàn)門如果是結(jié)構(gòu)體,我們無(wú)法對(duì)其做>操作。當(dāng)然解決這個(gè)問(wèn)題的方法也是有的—顯式具體化函數(shù)。
4.顯式具體化函數(shù)
顯式具體化函數(shù)的誕生是因?yàn)槟0鍖?duì)于某些類型的數(shù)據(jù),定義得的函數(shù),例如上例中得foo(c,d)
出錯(cuò),我們就單獨(dú)對(duì)這個(gè)類型,寫(xiě)一個(gè)特殊的函數(shù)。
所以,就是一句話,原先模板不適用于某種類型的數(shù)據(jù),我們就單獨(dú)給這種類型的數(shù)據(jù),單獨(dú)來(lái)一個(gè)函數(shù)定義。
#include<iostream> using namespace std; struct apple{ string name; double weight; int group; }; template <typename T> void Swap(T &a,T &b);//模板原型 template<> void Swap<apple>(apple &a,apple &b);//顯式具體化函數(shù)原型,這里<apple>可以省略 void show(apple x); int main(){ int a,b; a=1; b=2; Swap(a,b); cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; apple c={"Alice",200,1}; apple d={"Bob",250,2}; Swap(c,d); cout<<"c:"<<endl; show(c); cout<<"d:"<<endl; show(d); } template <typename T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; } template<> void Swap<apple>(apple &a,apple &b){ cout<<"explicit specialization for apple!"<<endl; int temp; temp=a.group; a.group=b.group; b.group=temp; } void show(apple x){ cout<<"name:"<<x.name<<endl; cout<<"weight:"<<x.weight<<endl; cout<<"group:"<<x.group<<endl; }
a:2
b:1
explicit specialization for apple!
c:
name:Alice
weight:200
group:2
d:
name:Bob
weight:250
group:1
可以看出來(lái),我們單獨(dú)為 結(jié)構(gòu)體apple
搞了個(gè)顯式具體化函數(shù),目的就是只交換group成員變量。
顯式具體化函數(shù)和常規(guī)模板很類似。
顯式具體化函數(shù)的原型:
template<>
void Swap<apple>(apple &a,apple &b);
這里<apple>
可以省略.
顯式具體化函數(shù)的定義:
template<> void Swap<apple>(apple &a,apple &b){ cout<<"explicit specialization for apple!"<<endl; int temp; temp=a.group; a.group=b.group; b.group=temp; }
實(shí)際上這段代碼也意味著,顯式具體化的優(yōu)先級(jí)高于常規(guī)模板。
5.實(shí)例化和具體化
切記!函數(shù)模板本身不會(huì)生成函數(shù)定義,它只是一個(gè)生成函數(shù)定義的方案!
編譯器使用模板為特定類型生成函數(shù)定義時(shí),得到的是模板實(shí)例。生成函數(shù)定義就是實(shí)例化。
實(shí)例化有隱式和顯式之分。
隱式實(shí)例化:
Swap(a,b);
或者Swap<int>(a,b);
隱式實(shí)例化是指等你調(diào)用了這個(gè)函數(shù)的時(shí)候,它才會(huì)生成函數(shù)定義。
顯式實(shí)例化:
template void Swap<int>(int,int);
顯式實(shí)例化是指不需要等你調(diào)用這個(gè)函數(shù),使用上面那段代碼,直接能生成Swap<int>
函數(shù)的定義。 一般來(lái)說(shuō),我們會(huì)把模板放到一個(gè)頭文件中,然后很多源文件會(huì)include它,然后編譯的時(shí)候就會(huì)在這些源文件中生成具體化的代碼。但是如果我們采用顯式實(shí)例化,在其中一個(gè)源文件里面實(shí)例化一份代碼,然后其他cpp文件用到的時(shí)候,通過(guò)鏈接程序找到這個(gè)代碼并調(diào)用它,程序的大小就會(huì)少一些。這就是顯式實(shí)例化的好處。
下面這段代碼展示了Add<double>(a,b)
相較于Add(a,b)
的優(yōu)越性:
#include<iostream> using namespace std; template <typename T> T Add(const T &a,const T &b){ return (a+b); } int main(){ int a=5; double b=6.1; cout<<Add<double>(a,b)<<endl; }
如果把Add<double>(a,b)
換成Add(a,b)
會(huì)出錯(cuò),因?yàn)閍是int類型的,而b是double類型的,這樣就無(wú)法隱式實(shí)例化了。Add<double>(a,b)
會(huì)實(shí)例化一個(gè)函數(shù)定義,然后int類型的a,傳參給double的引用形參的時(shí)候,會(huì)產(chǎn)生臨時(shí)變量,從而完成函數(shù)調(diào)用??傊?最好使用<type>
而不是根據(jù)參數(shù)類型自動(dòng)生成模板的實(shí)例化.
顯式隱式實(shí)例化和顯式具體化統(tǒng)稱為具體化或者實(shí)例化
上一節(jié)中我們提到了顯式具體化,我們可以發(fā)現(xiàn)實(shí)例化和顯式具體化的相同之處在于,他們都是使用具體類型的函數(shù)定義,而不是通用描述。
顯式具體化函數(shù)是否是模板? 我的回答是:顯式具體化函數(shù)是一個(gè)特殊的模板,它是專門為一種類型設(shè)計(jì)的模板。
//函數(shù)模板6.cpp #include<iostream> using namespace std; struct apple{ string name; double weight; int group; }; template<class T> void Swap(T &a,T &b);//模板函數(shù)原型 template<>void Swap(apple &a,apple &b);//顯式具體化原型 template void Swap<char>(char&,char&);//顯式實(shí)例化 void show(apple x); int main(){ short a=1; short b=2; Swap(a,b);//隱式實(shí)例化 cout<<"a:"<<a<<endl<<"b:"<<b<<endl; apple c={"Alice",200,1}; apple d={"Bob",250,2}; Swap(c,d);//顯式具體化 cout<<"c:"<<endl; show(c); cout<<"d:"<<endl; show(d); char e='a'; char f='b'; Swap<char>(e,f);//調(diào)用顯式實(shí)例化函數(shù) cout<<"e:"<<e<<endl<<"f:"<<f<<endl; } template<> void Swap(apple &a,apple &b){ int temp; temp=a.group; a.group=b.group; b.group=temp; } void show(apple x){ cout<<"name:"<<x.name<<endl; cout<<"weight:"<<x.weight<<endl; cout<<"group:"<<x.group<<endl; } template<class T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; }
a:2
b:1
c:
name:Alice
weight:200
group:2
d:
name:Bob
weight:250
group:1
e:2.01
f:1
這里問(wèn)個(gè)問(wèn)題,如果把上面代碼中的e變成 int類型會(huì)出現(xiàn)問(wèn)題嗎?
會(huì)報(bào)錯(cuò),因?yàn)閷?shí)參int
和函數(shù)中引用形參char&
的類型不一樣,且此時(shí)不是const引用形參,也不會(huì)有臨時(shí)變量產(chǎn)生。如果你不清楚,且看引用變量的語(yǔ)法。 內(nèi)聯(lián)函數(shù)、引用變量、函數(shù)重載
6.重載解析
6.1 概覽
對(duì)于常規(guī)函數(shù),函數(shù)重載,函數(shù)模板,函數(shù)模板重載,編譯器需要有一個(gè)良好的策略,從一大堆同名函數(shù)中選擇一個(gè)最佳函數(shù)定義。這一過(guò)程是非常復(fù)雜的過(guò)程–重載解析。這就是我們這一節(jié)要闡述的內(nèi)容。
重載解析過(guò)程:
- step1:創(chuàng)建候選函數(shù)列表。其中包含與被調(diào)用函數(shù)名稱相同的函數(shù)和模板函數(shù)。
- step2:從候選函數(shù)列表中篩選可行函數(shù)。其中包括參數(shù)正確或者隱式轉(zhuǎn)換后參數(shù)正確的函數(shù)。
- step3:確定是否存在最佳的可行函數(shù)。如果有則使用他,否則函數(shù)調(diào)用出錯(cuò)。
其中最復(fù)雜的就是step3,這些可行函數(shù)也有優(yōu)先級(jí)之分,優(yōu)先級(jí) 從高到低是:
- 完全匹配
- 提升轉(zhuǎn)化 (如,char short 轉(zhuǎn)化成int,float 轉(zhuǎn)化成 double)
- 標(biāo)準(zhǔn)轉(zhuǎn)化 (如,int 轉(zhuǎn)化成 char ,long轉(zhuǎn)化成double)
- 用戶定義的轉(zhuǎn)化 (如類聲明中定義的轉(zhuǎn)換)
而完全匹配中也有細(xì)小的優(yōu)先級(jí)之分。
總而言之,在step3
中如果優(yōu)先級(jí)最高的可行函數(shù)是唯一的那么就調(diào)用他,否則會(huì)出現(xiàn)諸如ambiguous
的錯(cuò)誤。
這一節(jié)的目的就是完全理解編譯器如何讓處理如下代碼:
#include<iostream> using namespace std; void may(int);//#1 float may(float,float=3);//#2存在默認(rèn)參數(shù) void may(char &);//#3 char* may(const char*);//#4 char may(const char &);//#5 template<class T> void may(const T &);//#6 template<class T> void may(T *);//#7 int main(){ may('B'); } void may(int a){ cout<<1<<endl; } float may(float a,float b){ cout<<2<<endl; return a; } void may(char &a){ cout<<3<<endl; } char* may(const char* a){ cout<<4<<endl; return NULL; } char may(const char &a){ cout<<5<<endl; return a; } template<class T> void may(const T & a){ cout<<6<<endl; } template<class T> void may(T *){ cout<<7<<endl; }
上述代碼沒(méi)有一點(diǎn)問(wèn)題,甚至連warning都沒(méi)有,你可以自己試一下結(jié)果是什么。
'B'
是const char類型的
#1~#7都是候選函數(shù),因?yàn)楹瘮?shù)名字相同。
其中#1、#2、#3、#5、#6是可行函數(shù),因?yàn)閏onst char 類型無(wú)法隱式轉(zhuǎn)換成指針類型,所以#4、#7不行,而其他函數(shù)通過(guò)隱式轉(zhuǎn)換后參數(shù)是正確的。
#1是提升轉(zhuǎn)換,#2是標(biāo)準(zhǔn)轉(zhuǎn)換,#3、#5、#6是完全匹配,完全匹配中非模板函數(shù)比模板函數(shù)優(yōu)先級(jí)高,所以#3、#5優(yōu)先級(jí)高于#6,而由于const參數(shù)優(yōu)先和const引用參數(shù)匹配,所以#5的優(yōu)先級(jí)更高。
則#5>#3>#6>#1>#2,所以調(diào)用#5。
6.2 完全匹配中的三六九等
首先什么是完全匹配?
完全匹配函數(shù)包括:
- 不需要進(jìn)行隱式類型轉(zhuǎn)化的函數(shù)(即參數(shù)正確的函數(shù))顯然是完全匹配函數(shù)。
- 需要進(jìn)行隱式類型轉(zhuǎn)換,但是這些轉(zhuǎn)換是無(wú)關(guān)緊要轉(zhuǎn)換。
完全匹配允許的無(wú)關(guān)緊要轉(zhuǎn)換:
實(shí) 參 | 形 參 |
---|---|
Type | Type& |
Typc& | Type |
Type[] | * Type |
Type (argument-list) | Type ( * ) (argument-list) |
Type | const Type |
Type | volatile Type |
Type * | const Type |
Type* | volatile Type * |
完全匹配中的優(yōu)先級(jí)法則
- 常規(guī)函數(shù)優(yōu)先級(jí)高于模板。
- 對(duì)于形參是指針或引用類型的函數(shù),const修飾的實(shí)參優(yōu)先匹配const修飾的形參,非const修飾的實(shí)參優(yōu)先匹配非const修飾的形參。
- 較具體的模板優(yōu)先級(jí)高于較簡(jiǎn)略的模板。(例如,顯式具體化函數(shù)優(yōu)先級(jí)高于常規(guī)模板)
#include<iostream> using namespace std; struct apple{ string name; double weight; int group; }; void may(const apple & a){ cout<<1<<endl; } void may(apple &a){ cout<<2<<endl; } int main(){ apple a={"Alice",250.00,1}; may(a); }
結(jié)果是2
#include<iostream> using namespace std; struct apple{ string name; double weight; int group; }; void may(const apple & a){ cout<<1<<endl; } void may(apple &a){ cout<<2<<endl; } void may(apple a){ cout<<3<<endl; } int main(){ apple a={"Alice",250.00,1}; may(a); }
這個(gè)編譯器會(huì)出錯(cuò),因?yàn)檫@三個(gè)函數(shù)都是完全匹配,但是#2 和 #3的優(yōu)先級(jí)無(wú)法區(qū)別,記得嗎,完全匹配中的優(yōu)先級(jí)法則的第2條法則,只適用于形參是引用或者指針。
#include<iostream> using namespace std; struct apple{ string name; double weight; int group; }; template<typename T> void may(T a){ cout<<1<<endl; } template<typename T> void may(T *a){ cout<<2<<endl; } int main(){ apple a={"Alice",250.00,1}; may(&a); }
終端輸出是2,&a
的類型是 apple*
,而#2明確指出形參是個(gè)指針,所以#2更具體。
關(guān)于如何找出最具體的模板的規(guī)則被稱為部分排序規(guī)則。
部分排序規(guī)則:在實(shí)例化過(guò)程中,函數(shù)優(yōu)先和轉(zhuǎn)換少的模板匹配。也可以這么說(shuō),實(shí)參和形參越相似,模板越優(yōu)先。
舉個(gè)栗子:
#include<iostream> using namespace std; template<typename T> void may(T a[]){ cout<<1<<endl; } template<typename T> void may(T *a[]){ cout<<2<<endl; } template<typename T> void may(const T *a[]){ cout<<3<<endl; } int main(){ double a[5]={1,2,3,4,5}; const double* b[5]={&a[0],&a[1],&a[2],&a[3],&a[4]}; may(a); may(b); }
may(a)
會(huì)和#1匹配,因?yàn)閍的類型是double數(shù)組,double數(shù)組無(wú)法轉(zhuǎn)換成指針數(shù)組,所以#2,#3不是可行函數(shù)。而對(duì)于may(b)
,他會(huì)和#3匹配。b的類型是cont指針數(shù)組,首先#1和#2和#3都是可行函數(shù),而且都是完全匹配函數(shù),因?yàn)?1 會(huì)實(shí)例化成may<const double*>(b)
,#2 他實(shí)例化成may<const double>(b)
,#3會(huì)實(shí)例化為may<double>(b)
所以我們看看那個(gè)模板更具體?#3模板直接指出了 形參是一個(gè)const指針數(shù)組,所以他最具體,#3優(yōu)先級(jí)最高;其次是#2因?yàn)樗男螀⒅赋隽耸侵羔様?shù)組;#1是最不具體的,#3>#2>#1.
6.3 總結(jié)
可行函數(shù)中優(yōu)先級(jí)從高到低排列 | ||
---|---|---|
完全匹配 | 常規(guī)函數(shù) | 形參若是指針或引用,注意const和非const |
模板 | 較具體的模板優(yōu)先級(jí)更高 | |
提升轉(zhuǎn)換 | ||
標(biāo)準(zhǔn)轉(zhuǎn)換 | ||
用戶定義轉(zhuǎn)換 |
Swap<>(a,b)
這種代碼,類似于顯式實(shí)例化,但是<>中沒(méi)有指出typename,所以這段代碼是要求優(yōu)先選擇模板函數(shù)。
對(duì)于多參數(shù)的函數(shù),優(yōu)先級(jí)會(huì)非常復(fù)雜,就不談了。
7.模板的發(fā)展
關(guān)鍵字decltype 和 auto
#include<iostream>using namespace std;template<typename T1,typename T2>auto Add(T1 a, T2 b){ decltype(a+b) c; c=a+b; return c;}int main(){ int a=2; double b=2.123; cout<<Add(a,b);}#include<iostream> using namespace std; template<typename T1,typename T2> auto Add(T1 a, T2 b){ decltype(a+b) c; c=a+b; return c; } int main(){ int a=2; double b=2.123; cout<<Add(a,b); }
關(guān)鍵字decltype 和 auto ,在模板中無(wú)法確定數(shù)據(jù)類型時(shí),發(fā)揮了巨大的作用。
到此這篇關(guān)于C++函數(shù)模板與重載解析超詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++函數(shù)模板與重載解析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章帶你了解C語(yǔ)言內(nèi)存對(duì)齊解決的問(wèn)題
內(nèi)存對(duì)齊的目的是為了提高CPU讀寫(xiě)內(nèi)存里數(shù)據(jù)的速度?,F(xiàn)代的CPU讀取內(nèi)存并不是一個(gè)一個(gè)字節(jié)挨著讀取,這樣做的效率非常低?,F(xiàn)代的CPU一般以4個(gè)字節(jié)(32bit數(shù)據(jù)總線)或者8個(gè)字節(jié)(64bit數(shù)據(jù)總線)為一組,一組一組地讀寫(xiě)內(nèi)存里的數(shù)據(jù)2021-08-08C語(yǔ)言設(shè)計(jì)前中后隊(duì)列實(shí)例代碼
隊(duì)列最主要的作用就是用來(lái)管理數(shù)據(jù)流的,防止數(shù)據(jù)因?yàn)閭鬏旑l率過(guò)快得不到及時(shí)處理而丟失,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言設(shè)計(jì)前中后隊(duì)列的相關(guān)資料,需要的朋友可以參考下2021-12-12c++實(shí)現(xiàn)合并文件以及拆分實(shí)例代碼
這篇文章主要介紹了c++實(shí)現(xiàn)合并文件以及拆分實(shí)例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單三子棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單三子棋游戲,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07使用C/C++語(yǔ)言生成一個(gè)隨機(jī)迷宮游戲
迷宮相信大家都走過(guò),主要是考驗(yàn)?zāi)愕倪壿嬎季S。今天小編使用C語(yǔ)言生成一個(gè)隨機(jī)迷宮游戲,具體實(shí)現(xiàn)代碼,大家通過(guò)本文學(xué)習(xí)吧2016-12-12C++使用ImGUI框架開(kāi)發(fā)一個(gè)簡(jiǎn)單程序
ImGui?是一個(gè)用于C++的用戶界面庫(kù),跨平臺(tái)、無(wú)依賴,支持OpenGL、DirectX等多種渲染API,下面就跟隨小編一起學(xué)習(xí)一下如何使用ImGUI框架開(kāi)發(fā)一個(gè)簡(jiǎn)單程序吧2023-08-08