青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

woaidongmao

文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
數(shù)據(jù)加載中……

為什么不要特化函數(shù)模版

在C++中有class templatesfunction templates,這兩種模版有很多區(qū)別,最重要的區(qū)別就是重載(overloading):
普通的C++類不能重載,當(dāng)然類模版也不能重載;相反,普通函數(shù)可以重載,函數(shù)模版也能重載。這再正常不過,看下面的代碼:

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。當(dāng)然,base templates能夠被特化,在特化這一點(diǎn)上
class templatesfunction templates有很大的區(qū)別:一個(gè)class template 能夠被partially specialized and/or
fully specialized,一個(gè)function template只能被fully specialized,但是由于function templates能夠重載我們可以通過重載來實(shí)現(xiàn)和partially specialized 相當(dāng)?shù)墓δ堋O旅娴拇a說明了這些區(qū)別:

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 

根據(jù)函數(shù)重載解析規(guī)則:

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)

上面說的這些其實(shí)都是很簡(jiǎn)單的情況,大多數(shù)人很容易就能明白,下面的才是容易讓人弄混的:

1.考慮如下代碼:

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)

最后一行的結(jié)果像大多數(shù)人所期望的一樣,問題是:為什么期望是這個(gè)結(jié)果?
如果你期望的原因是錯(cuò)誤的,接下來的一定會(huì)讓你好奇。也許你會(huì)說:"我為int*寫了一個(gè)特化版本,f(p)當(dāng)然會(huì)調(diào)用c",不幸的是,這正是錯(cuò)誤的原因!!!

2.再考慮下面的代碼:

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

如果這個(gè)結(jié)果讓你感到驚奇,那就對(duì)了!很多人都會(huì)感到驚奇!
理解這個(gè)的關(guān)鍵是:Specializations don't overload,only the base templates overload.

重載解析僅僅選擇base template(或者nontemplate function,如果有的話),只有當(dāng)編譯器已經(jīng)決定了哪個(gè)
base template將會(huì)被選擇,編譯器才會(huì)繼續(xù)往下尋找適合的特化版本,如果找到了就使用那個(gè)特化版本。

最后,應(yīng)當(dāng)避免特化函數(shù)模板,也要避免重載函數(shù)模板(nontemplate function的重載當(dāng)然沒問題)。如果一定要這樣,可以使用如下方法模擬函數(shù)模板的偏特化:

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

posted on 2008-10-13 11:26 肥仔 閱讀(309) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ 模板

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久五月天婷婷| 西西人体一区二区| 国产精品r级在线| 欧美日本国产一区| 欧美日韩精品久久久| 亚洲一区二区3| 国产精品久久国产精麻豆99网站| 欧美区二区三区| 欧美日韩精品一本二本三本| 欧美日韩一区二区在线播放| 国产精品美女主播| 国产一区清纯| 亚洲精品日韩综合观看成人91| 亚洲激情网站| 亚洲欧美国产毛片在线| 久久国产精品久久w女人spa| 久久天天躁夜夜躁狠狠躁2022| 欧美成ee人免费视频| 亚洲国产一二三| 亚洲美女啪啪| 欧美一区二区三区四区在线观看地址 | 欧美成人一区在线| 欧美日本中文字幕| 国产精品无人区| 亚洲第一级黄色片| 亚洲午夜精品久久久久久app| 一区免费在线| 欧美日韩国产区一| 99视频精品免费观看| 欧美激情麻豆| 国产精品久久久久久超碰 | 亚洲永久免费av| 久久精品观看| 亚洲精品色婷婷福利天堂| 一本一本a久久| 久久高清一区| 国产精品九九| 亚洲精品免费一二三区| 久久福利影视| 一区二区三区视频免费在线观看| 久久久久久亚洲精品杨幂换脸| 欧美三级特黄| 最新中文字幕亚洲| 久久九九精品99国产精品| 洋洋av久久久久久久一区| 久久久亚洲国产天美传媒修理工 | 亚洲亚洲精品三区日韩精品在线视频| 欧美亚洲综合在线| 欧美日韩在线免费| 亚洲欧洲在线免费| 老司机免费视频一区二区| 亚洲影音先锋| 欧美日韩另类字幕中文| 亚洲欧洲在线观看| 欧美黄色aaaa| 久久男人av资源网站| 国产麻豆一精品一av一免费| 一区二区久久| 亚洲精品一区二区三区99| 久久一区中文字幕| 在线视频观看日韩| 欧美成人免费全部| 久久久久免费观看| 1204国产成人精品视频| 久久亚洲影音av资源网| 久久国产视频网站| 精品不卡在线| 欧美成年视频| 欧美fxxxxxx另类| 亚洲精品久久久久久久久| 亚洲黄色影院| 欧美日韩国产精品专区| 亚洲视频在线观看视频| 亚洲国内精品| 欧美日韩你懂的| 日韩一区二区电影网| 亚洲精选中文字幕| 国产精品成人aaaaa网站| 亚洲一区二区三区在线视频| 一区二区三区四区蜜桃| 国产精品日韩一区| 久久成人免费网| 久久精品国产亚洲精品| 在线日韩电影| 日韩亚洲综合在线| 国产女人18毛片水18精品| 久久一区二区三区av| 女人天堂亚洲aⅴ在线观看| 中文欧美在线视频| 亚洲欧美日韩国产一区二区三区| 国产一区二区视频在线观看| 欧美成人在线免费观看| 欧美日本中文字幕| 久久爱91午夜羞羞| 欧美91精品| 欧美亚洲免费| 免费日韩成人| 久久国产精品一区二区三区四区 | 91久久精品网| 国产欧美视频在线观看| 亚洲第一久久影院| 国产欧美一区二区视频| 亚洲国产成人在线视频| 国产精品乱码久久久久久| 亚洲美女av黄| 国产精品免费看久久久香蕉| 亚洲一区欧美激情| 亚洲精品在线看| 鲁大师影院一区二区三区| 中文国产成人精品| 亚洲国产天堂久久国产91| 国产精品久久久久婷婷| 久久精品99国产精品日本| 欧美成人亚洲成人| 久久九九热免费视频| 亚洲欧美日韩一区二区三区在线观看 | 国产欧美午夜| 欧美在线高清| 欧美aaa级| 久久亚洲综合| 国产精品―色哟哟| 亚洲三级国产| 在线不卡中文字幕| 性欧美激情精品| 在线视频精品| 欧美大片在线看免费观看| 久久久中精品2020中文| 国产精品永久免费| 一本色道久久综合狠狠躁篇怎么玩| 影音先锋日韩精品| 欧美亚洲一区二区在线观看| 亚洲一区二区三区欧美| 欧美精品一区视频| 欧美激情精品| 亚洲国产精品久久久久| 久久精品一区二区三区四区| 欧美专区第一页| 国产伦精品一区二区三区在线观看 | 欧美刺激性大交免费视频| 国产欧美在线播放| 午夜精品一区二区三区电影天堂 | 亚洲国产mv| 老司机成人在线视频| 久久综合久久综合久久综合| 国产日韩欧美高清| 性做久久久久久免费观看欧美 | 欧美激情久久久| 欧美va日韩va| 亚洲国产精品久久久久秋霞蜜臀 | 国产亚洲欧美另类中文| 欧美影院视频| 麻豆国产va免费精品高清在线| 国产亚洲精品久久久久久| 日韩亚洲成人av在线| av成人激情| 欧美日韩一区不卡| 亚洲淫性视频| 狼人社综合社区| 亚洲国产一区二区三区高清| 久久亚洲春色中文字幕| 亚洲国产日日夜夜| 在线视频免费在线观看一区二区| 欧美日产在线观看| 一本一本大道香蕉久在线精品| 在线视频欧美一区| 国产精品美女久久久| 午夜精品www| 欧美成人精品三级在线观看 | 海角社区69精品视频| 免费一级欧美片在线播放| 日韩小视频在线观看| 欧美一区三区三区高中清蜜桃| 亚洲系列中文字幕| 欧美在线播放视频| 欧美三级免费| 国产精品99久久久久久久vr| 日韩一级片网址| 亚洲一品av免费观看| 欧美日韩一区在线播放| 亚洲网站在线| 欧美不卡在线| 亚洲欧美国产制服动漫| 黄色亚洲精品| 欧美日韩视频在线| 亚洲一区精品视频| 美女国产精品| 亚洲桃花岛网站| 一区二区三区在线视频播放| 欧美成人精品1314www| 9人人澡人人爽人人精品| 久久婷婷av| 午夜老司机精品| 亚洲国产高潮在线观看| 国产精品免费视频xxxx| 久久精品一区二区三区四区| 亚洲精品一区二区在线观看| 久久久精品国产免大香伊| 在线一区二区日韩| 国产婷婷97碰碰久久人人蜜臀| 欧美黄色aa电影|