Overloading vs. Specialization
鍦–++涓湁class templates 鍜?em>function templates錛岃繖涓ょ妯$増鏈夊緢澶氬尯鍒紝鏈閲嶈鐨勫尯鍒氨鏄噸杞?overloading)錛?br />鏅氱殑C++綾諱笉鑳介噸杞斤紝褰撶劧綾繪ā鐗堜篃涓嶈兘閲嶈澆錛涚浉鍙嶏紝鏅氬嚱鏁板彲浠ラ噸杞斤紝鍑芥暟妯$増涔熻兘閲嶈澆銆傝繖鍐嶆甯鎬笉榪囷紝鐪嬩笅闈㈢殑浠g爜錛?/p>
聽1聽//
聽Example聽1:聽Class聽vs.聽function聽template,聽and聽overloading聽
聽2聽//
聽3聽
聽4聽//聽A聽class聽template
聽5聽template<class聽T>聽class聽X聽{聽/*...*/聽};聽聽聽聽聽聽//
聽(a)
聽6聽
聽7聽//聽A聽function聽template聽with聽two聽overloads
聽8聽template<class聽T>聽void聽f(聽T聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽(b)
聽9聽template<class聽T>聽void聽f(聽int,聽T,聽double聽);聽//
聽(c)
10聽
鍍忎笂闈㈡湭鐗瑰寲鐨勬ā鏉塊氬父鍙仛base templates銆傚綋鐒訛紝base templates鑳藉琚壒鍖栵紝鍦ㄧ壒鍖栬繖涓鐐逛笂
class templates 鍜?em>function templates鏈夊緢澶х殑鍖哄埆錛氫竴涓?em>class template 鑳藉琚?em>partially specialized and/or
fully specialized錛屼竴涓?em>function template鍙兘琚?em>fully specialized錛屼絾鏄敱浜?em>function templates鑳藉閲嶈澆鎴戜滑鍙互閫氳繃閲嶈澆鏉ュ疄鐜板拰partially specialized 鐩稿綋鐨勫姛鑳姐備笅闈㈢殑浠g爜璇存槑浜嗚繖浜涘尯鍒細(xì)
聽1聽//
聽Example聽1,聽continued:聽Specializing聽templates聽
聽2聽//
聽3聽
聽4聽//聽A聽partial聽specialization聽of聽(a)聽for聽pointer聽types聽
聽5聽template<class聽T>聽class聽X<T*>聽{聽/*...*/
聽};
聽6聽
聽7聽//聽A聽full聽specialization聽of聽(a)聽for聽int聽
聽8聽template<>聽class聽X<int>聽{聽/*...*/
聽};
聽9聽
10聽//
聽A聽separate聽base聽template聽that聽overloads聽(b)聽and聽(c)聽
11聽//
聽--聽NOT聽a聽partial聽specialization聽of聽(b),聽because聽
12聽//
聽there's聽no聽such聽thing聽as聽a聽partial聽specialization聽
13聽//聽of聽a聽function聽template!聽
14聽template<class聽T>聽void聽f(聽T*聽);聽聽聽聽聽聽聽聽聽聽聽聽聽//
聽(d)
15聽
16聽//聽A聽full聽specialization聽of聽(b)聽for聽int聽
17聽template<>聽void聽f<int>(聽int聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽//
聽(e)
18聽
19聽//
聽A聽plain聽old聽function聽that聽happens聽to聽overload聽with聽
20聽//
聽(b),聽(c),聽and聽(d)聽--聽but聽not聽(e),聽which聽we'll聽
21聽//聽discuss聽in聽a聽moment聽
22聽void聽f(聽double聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//
聽(f)
23聽
鏍規(guī)嵁鍑芥暟閲嶈澆瑙f瀽瑙勫垯錛?/p>
聽1聽//
聽Example聽1,聽continued:聽Overload聽resolution聽
聽2聽//聽
聽3聽
bool聽b;聽
聽4聽int
聽i;聽
聽5聽double
聽d;
聽6聽
聽7聽f(聽b聽);聽聽聽聽聽聽聽聽//聽calls聽(b)聽with聽T聽=聽bool聽
聽8聽f(聽i,聽42,聽d聽);聽//聽calls聽(c)聽with聽T聽=聽int聽
聽9聽f(聽&i聽);聽聽聽聽聽聽聽//聽calls聽(d)聽with聽T聽=聽int聽
10聽f(聽i聽);聽聽聽聽聽聽聽聽//聽calls聽(e)聽
11聽f(聽d聽);聽聽聽聽聽聽聽聽//聽calls聽(f)
涓婇潰璇寸殑榪欎簺鍏跺疄閮芥槸寰堢畝鍗曠殑鎯呭喌錛屽ぇ澶氭暟浜哄緢瀹規(guī)槗灝辮兘鏄庣櫧錛屼笅闈㈢殑鎵嶆槸瀹規(guī)槗璁╀漢寮勬販鐨勶細(xì)
1.鑰冭檻濡備笅浠g爜錛?/p>
聽1聽//
聽Example聽2:聽Explicit聽specialization聽
聽2聽//聽
聽3聽template<class聽T>聽//聽(a)聽a聽base聽template聽
聽4聽void
聽f(聽T聽);
聽5聽
聽6聽template<class聽T>聽//聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽
聽7聽void聽f(聽T*聽);聽聽聽聽聽//
聽聽聽聽聽(function聽templates聽can't聽be聽partially聽
聽8聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽specialized;聽they聽overload聽instead)
聽9聽
10聽template<>聽聽聽聽聽聽聽聽//聽(c)聽explicit聽specialization聽of聽(b)聽
11聽void聽f<>(int*
);
12聽
13聽//聽...
14聽
15聽int聽*
p;聽
16聽f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽//聽calls聽(c)
鏈鍚庝竴琛岀殑緇撴灉鍍忓ぇ澶氭暟浜烘墍鏈熸湜鐨勪竴鏍鳳紝闂鏄細(xì)涓轟粈涔堟湡鏈涙槸榪欎釜緇撴灉錛?br />濡傛灉浣犳湡鏈涚殑鍘熷洜鏄敊璇殑錛屾帴涓嬫潵鐨勪竴瀹氫細(xì)璁╀綘濂藉銆備篃璁鎬綘浼?xì)璇村Q?鎴戜負(fù)int*鍐欎簡涓涓壒鍖栫増鏈紝f(p)褰撶劧浼?xì)璋冪?span style="COLOR: #3366ff">c"錛屼笉騫哥殑鏄紝榪欐鏄敊璇殑鍘熷洜!!!
2.鍐嶈冭檻涓嬮潰鐨勪唬鐮侊細(xì)
聽1聽//
聽Example聽3
聽2聽//聽
聽3聽template<class聽T>聽//聽(a)聽same聽old聽base聽template聽as聽before聽
聽4聽void
聽f(聽T聽);
聽5聽
聽6聽template<>聽聽聽聽聽聽聽聽//聽(c)聽explicit聽specialization,聽this聽time聽of聽(a)
聽7聽void聽f<>(int*
);
聽8聽
聽9聽template<class聽T>聽//聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽
10聽void聽f(聽T*
聽);
11聽
12聽//聽...
13聽
14聽int聽*
p;聽
15聽f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽//
聽calls聽(b)!聽overload聽resolution聽ignores聽
16聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//
聽specializations聽and聽operates聽on聽the聽base聽
17聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//聽function聽templates聽only
濡傛灉榪欎釜緇撴灉璁╀綘鎰熷埌鎯婂錛岄偅灝卞浜嗭紒寰堝浜洪兘浼?xì)鎰熷埌鎯婂锛?br />鐞嗚В榪欎釜鐨勫叧閿槸錛?em style="COLOR: #993300">Specializations don't overload,only the base templates overload.
閲嶈澆瑙f瀽浠呬粎閫夋嫨base template(鎴栬卬ontemplate function錛屽鏋滄湁鐨勮瘽)錛屽彧鏈夊綋緙栬瘧鍣ㄥ凡緇忓喅瀹氫簡鍝釜
base template灝嗕細(xì)琚夋嫨錛岀紪璇戝櫒鎵嶄細(xì)緇х畫寰涓嬪鎵鵑傚悎鐨勭壒鍖栫増鏈紝濡傛灉鎵懼埌浜嗗氨浣跨敤閭d釜鐗瑰寲鐗堟湰銆?br />
鏈鍚庯紝搴斿綋閬垮厤鐗瑰寲鍑芥暟妯℃澘錛屼篃瑕侀伩鍏嶉噸杞藉嚱鏁版ā鏉?nontemplate function鐨勯噸杞藉綋鐒舵病闂)銆傚鏋滀竴瀹氳榪欐牱錛屽彲浠ヤ嬌鐢ㄥ涓嬫柟娉曟ā鎷熷嚱鏁版ā鏉跨殑鍋忕壒鍖栵細(xì)
聽1聽//base聽template聽class,聽
聽2聽template聽<class聽T>
聽3聽
struct聽FuncImpl聽{
聽4聽聽聽聽聽//users,聽go聽ahead聽and聽specialize聽this
聽5聽聽聽聽聽static聽int聽apply(const聽T聽&
t)聽{
聽6聽聽聽聽聽聽聽聽聽return聽0
;
聽7聽
聽聽聽聽}
聽8聽
};
聽9聽
10聽//partial聽specialazation聽for聽int
11聽template聽<>
12聽struct聽FuncImpl<int>
聽{
13聽聽聽聽聽static聽int聽apply(int
聽t)聽{
14聽聽聽聽聽聽聽聽聽return聽1
;
15聽
聽聽聽聽}
16聽
};
17聽
18聽//partial聽specialazation聽for聽T*
19聽template聽<class聽T>
20聽聽聽聽聽struct聽FuncImpl<T聽*>
聽{
21聽聽聽聽聽static聽int聽apply(T聽*
t)聽{
22聽聽聽聽聽聽聽聽聽return聽2
;
23聽
聽聽聽聽}
24聽
};
25聽
26聽//users,聽don't聽touch聽this!
27聽template聽<class聽T>
28聽int聽func(const聽T聽&
t)聽{
29聽聽聽聽聽return聽FuncImpl<T>
::apply(t);
30聽
}
31聽
32聽int聽i聽=聽10
,聽r;
33聽r聽=聽func('c');聽//r聽=聽0
34聽r聽=聽func(8);聽//r聽=聽1
35聽r聽=聽func(&i);聽//r聽=聽2

]]>