C++函數(shù)模板與重載解析超詳細(xì)講解
1.快速上手
函數(shù)模板是通用的函數(shù)描述,也就是說,它們使用泛型來定義函數(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
。
不過模板原型實際上不常見。
模板函數(shù)定義:
template <typename T> void Swap(T &a,T &b){ T temp; temp=a; a=b; b=temp; }
模板函數(shù)隱式實例化:
Swap(a,b);
模板函數(shù)會根據(jù)實參的類型,給出函數(shù)定義。 還有顯式實例化: Swap<int>(a,b);
顯式的定義typename。 對于這兩種實例化,我推薦使用顯式實例化,因為隱式實例化容易出錯。對于這塊知識的詳細(xì)解讀,需要有對編譯器有充分的理解,在文章后面會給出。
一般我們不會用到模板函數(shù)的原型,因為我們一般把模板函數(shù)的定義放在頭文件里面,再需要使用的時候,包含頭文件就行了。
不推薦的做法:模板原型放在頭文件,模板定義放在cpp文件里。
2.重載的模板
如果對函數(shù)的重載不了解,可以翻看我之前的文章:
內(nèi)聯(lián)函數(shù)、引用變量、函數(shù)重載
模板函數(shù)也可以重載,語法和常規(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; }
上面這段代碼是出錯的,因為T如果是結(jié)構(gòu)體,我們無法對其做>操作。當(dāng)然解決這個問題的方法也是有的—顯式具體化函數(shù)。
4.顯式具體化函數(shù)
顯式具體化函數(shù)的誕生是因為模板對于某些類型的數(shù)據(jù),定義得的函數(shù),例如上例中得foo(c,d)
出錯,我們就單獨(dú)對這個類型,寫一個特殊的函數(shù)。
所以,就是一句話,原先模板不適用于某種類型的數(shù)據(jù),我們就單獨(dú)給這種類型的數(shù)據(jù),單獨(dú)來一個函數(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
可以看出來,我們單獨(dú)為 結(jié)構(gòu)體apple
搞了個顯式具體化函數(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; }
實際上這段代碼也意味著,顯式具體化的優(yōu)先級高于常規(guī)模板。
5.實例化和具體化
切記!函數(shù)模板本身不會生成函數(shù)定義,它只是一個生成函數(shù)定義的方案!
編譯器使用模板為特定類型生成函數(shù)定義時,得到的是模板實例。生成函數(shù)定義就是實例化。
實例化有隱式和顯式之分。
隱式實例化:
Swap(a,b);
或者Swap<int>(a,b);
隱式實例化是指等你調(diào)用了這個函數(shù)的時候,它才會生成函數(shù)定義。
顯式實例化:
template void Swap<int>(int,int);
顯式實例化是指不需要等你調(diào)用這個函數(shù),使用上面那段代碼,直接能生成Swap<int>
函數(shù)的定義。 一般來說,我們會把模板放到一個頭文件中,然后很多源文件會include它,然后編譯的時候就會在這些源文件中生成具體化的代碼。但是如果我們采用顯式實例化,在其中一個源文件里面實例化一份代碼,然后其他cpp文件用到的時候,通過鏈接程序找到這個代碼并調(diào)用它,程序的大小就會少一些。這就是顯式實例化的好處。
下面這段代碼展示了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)
會出錯,因為a是int類型的,而b是double類型的,這樣就無法隱式實例化了。Add<double>(a,b)
會實例化一個函數(shù)定義,然后int類型的a,傳參給double的引用形參的時候,會產(chǎn)生臨時變量,從而完成函數(shù)調(diào)用??傊?最好使用<type>
而不是根據(jù)參數(shù)類型自動生成模板的實例化.
顯式隱式實例化和顯式具體化統(tǒng)稱為具體化或者實例化
上一節(jié)中我們提到了顯式具體化,我們可以發(fā)現(xiàn)實例化和顯式具體化的相同之處在于,他們都是使用具體類型的函數(shù)定義,而不是通用描述。
顯式具體化函數(shù)是否是模板? 我的回答是:顯式具體化函數(shù)是一個特殊的模板,它是專門為一種類型設(shè)計的模板。
//函數(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&);//顯式實例化 void show(apple x); int main(){ short a=1; short b=2; Swap(a,b);//隱式實例化 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ù) 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
這里問個問題,如果把上面代碼中的e變成 int類型會出現(xiàn)問題嗎?
會報錯,因為實參int
和函數(shù)中引用形參char&
的類型不一樣,且此時不是const引用形參,也不會有臨時變量產(chǎn)生。如果你不清楚,且看引用變量的語法。 內(nèi)聯(lián)函數(shù)、引用變量、函數(shù)重載
6.重載解析
6.1 概覽
對于常規(guī)函數(shù),函數(shù)重載,函數(shù)模板,函數(shù)模板重載,編譯器需要有一個良好的策略,從一大堆同名函數(shù)中選擇一個最佳函數(shù)定義。這一過程是非常復(fù)雜的過程–重載解析。這就是我們這一節(jié)要闡述的內(nèi)容。
重載解析過程:
- step1:創(chuàng)建候選函數(shù)列表。其中包含與被調(diào)用函數(shù)名稱相同的函數(shù)和模板函數(shù)。
- step2:從候選函數(shù)列表中篩選可行函數(shù)。其中包括參數(shù)正確或者隱式轉(zhuǎn)換后參數(shù)正確的函數(shù)。
- step3:確定是否存在最佳的可行函數(shù)。如果有則使用他,否則函數(shù)調(diào)用出錯。
其中最復(fù)雜的就是step3,這些可行函數(shù)也有優(yōu)先級之分,優(yōu)先級 從高到低是:
- 完全匹配
- 提升轉(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)先級之分。
總而言之,在step3
中如果優(yōu)先級最高的可行函數(shù)是唯一的那么就調(diào)用他,否則會出現(xiàn)諸如ambiguous
的錯誤。
這一節(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; }
上述代碼沒有一點(diǎn)問題,甚至連warning都沒有,你可以自己試一下結(jié)果是什么。
'B'
是const char類型的
#1~#7都是候選函數(shù),因為函數(shù)名字相同。
其中#1、#2、#3、#5、#6是可行函數(shù),因為const char 類型無法隱式轉(zhuǎn)換成指針類型,所以#4、#7不行,而其他函數(shù)通過隱式轉(zhuǎn)換后參數(shù)是正確的。
#1是提升轉(zhuǎn)換,#2是標(biāo)準(zhǔn)轉(zhuǎn)換,#3、#5、#6是完全匹配,完全匹配中非模板函數(shù)比模板函數(shù)優(yōu)先級高,所以#3、#5優(yōu)先級高于#6,而由于const參數(shù)優(yōu)先和const引用參數(shù)匹配,所以#5的優(yōu)先級更高。
則#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)換是無關(guān)緊要轉(zhuǎn)換。
完全匹配允許的無關(guān)緊要轉(zhuǎn)換:
實 參 | 形 參 |
---|---|
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)先級法則
- 常規(guī)函數(shù)優(yōu)先級高于模板。
- 對于形參是指針或引用類型的函數(shù),const修飾的實參優(yōu)先匹配const修飾的形參,非const修飾的實參優(yōu)先匹配非const修飾的形參。
- 較具體的模板優(yōu)先級高于較簡略的模板。(例如,顯式具體化函數(shù)優(yōu)先級高于常規(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); }
這個編譯器會出錯,因為這三個函數(shù)都是完全匹配,但是#2 和 #3的優(yōu)先級無法區(qū)別,記得嗎,完全匹配中的優(yōu)先級法則的第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明確指出形參是個指針,所以#2更具體。
關(guān)于如何找出最具體的模板的規(guī)則被稱為部分排序規(guī)則。
部分排序規(guī)則:在實例化過程中,函數(shù)優(yōu)先和轉(zhuǎn)換少的模板匹配。也可以這么說,實參和形參越相似,模板越優(yōu)先。
舉個栗子:
#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)
會和#1匹配,因為a的類型是double數(shù)組,double數(shù)組無法轉(zhuǎn)換成指針數(shù)組,所以#2,#3不是可行函數(shù)。而對于may(b)
,他會和#3匹配。b的類型是cont指針數(shù)組,首先#1和#2和#3都是可行函數(shù),而且都是完全匹配函數(shù),因為#1 會實例化成may<const double*>(b)
,#2 他實例化成may<const double>(b)
,#3會實例化為may<double>(b)
所以我們看看那個模板更具體?#3模板直接指出了 形參是一個const指針數(shù)組,所以他最具體,#3優(yōu)先級最高;其次是#2因為它的形參指出了是指針數(shù)組;#1是最不具體的,#3>#2>#1.
6.3 總結(jié)
可行函數(shù)中優(yōu)先級從高到低排列 | ||
---|---|---|
完全匹配 | 常規(guī)函數(shù) | 形參若是指針或引用,注意const和非const |
模板 | 較具體的模板優(yōu)先級更高 | |
提升轉(zhuǎn)換 | ||
標(biāo)準(zhǔn)轉(zhuǎn)換 | ||
用戶定義轉(zhuǎn)換 |
Swap<>(a,b)
這種代碼,類似于顯式實例化,但是<>中沒有指出typename,所以這段代碼是要求優(yōu)先選擇模板函數(shù)。
對于多參數(shù)的函數(shù),優(yōu)先級會非常復(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 ,在模板中無法確定數(shù)據(jù)類型時,發(fā)揮了巨大的作用。
到此這篇關(guān)于C++函數(shù)模板與重載解析超詳細(xì)講解的文章就介紹到這了,更多相關(guān)C++函數(shù)模板與重載解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!