• <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>

            Edgard

            討論FunctionTemplate申明的隱藏性(Visibility)

            看看兩端代碼,區(qū)別重要在:
            一個(gè)是:inline int const& max申明在template <typename T>
            inline T const& max之前。


            // maximum of two int values
            inline int const& max (int const& a, int const& b)
            {
                return a<b?b:a;
            }

            // maximum of two values of any type
            template <typename T>
            inline T const& max (T const& a, T const& b)
            {
                return a<b?b:a;
            }

            // maximum of three values of any type
            template <typename T>
            inline T const& max (T const& a, T const& b, T const& c)
            {
                return max (max(a,b), c); 
            }                              

            一個(gè)是:inline int const& max申明在template <typename T>
            inline T const& max之后。

            // maximum of two values of any type
            template <typename T>
            inline T const& max (T const& a, T const& b)
            {
                return a<b?b:a;
            }

            // maximum of three values of any type
            template <typename T>
            inline T const& max (T const& a, T const& b, T const& c)
            {
                return max (max(a,b), c); 
            }

            // maximum of two int values
            inline int const& max (int const& a, int const& b)
            {
                return a<b?b:a;
            }

            調(diào)用程序:
            int main( )
            {
                  //當(dāng)然這里本來(lái)就寫(xiě)得不好,要先顯式申明寫(xiě)局部變量......
                  // 看你了解多少,討論討論兩種執(zhí)行可能的執(zhí)行路徑,即:FunctionTemplate的調(diào)用路徑!!!
                  ::max( 4, 10 ,15 );


            posted on 2005-12-15 21:10 Edgard 閱讀(514) 評(píng)論(5)  編輯 收藏 引用

            評(píng)論

            # re: 討論FunctionTemplate申明的隱藏性(Visibility) 2005-12-16 10:26 小明

            Function templates can be overloaded with nontemplate functions. All else being equal, the nontemplate function is preferred in Function templates can be overloaded with nontemplate functions. All else being equal, the nontemplate function is preferred in selecting the actual function being called. The following example illustrates this:

            // details/nontmpl.cpp

            #include <string>
            #include <iostream>

            template<typename T>
            std::string f(T)
            {
            return "Template";
            }

            std::string f(int&)
            {
            return "Nontemplate";
            }

            int main()
            {
            int x = 7;
            std::cout << f(x) << std::endl;
            }
            This should output:

            Nontemplate


            以上是C++ templates的原文。
            所以nontemplate的函數(shù)應(yīng)該被匹配調(diào)用,跟聲明的前后沒(méi)關(guān)系

              回復(fù)  更多評(píng)論   

            # re: 討論FunctionTemplate申明的隱藏性(Visibility) 2005-12-16 12:47 Edgard

            上面是我讀一本書(shū)測(cè)試過(guò)的例子,書(shū)中曾說(shuō):This is only one example of code that might behave differently than expected as a result of detailed overload resolution rules. For example, the fact that not all overloaded functions are visible when a corresponding function call is made may or may not matter. In fact, defining a three-argument version of max() without having seen the declaration of a special two-argument version of max() for ints causes the two-argument template to be used by the three-argument version:,這里的意思比較適合第二種情況,即”一個(gè)是:inline int const& max申明在template <typename T>
            inline T const& max之后。”,我沒(méi)有能在其他C++編譯器上測(cè)試過(guò),不知道C++標(biāo)準(zhǔn)中是否規(guī)定FunctionTemplate Resolution Rules,如果沒(méi)有,依據(jù)上面的英文描述,不同編譯器有不同的FunctionTemplate Resolution Rule,有可能template <typename T>
            inline T const& max (T const& a, T const& b, T const& c) 不可見(jiàn)到申明在后的inline int const& max。

            大家的意見(jiàn)呢?  回復(fù)  更多評(píng)論   

            # re: 討論FunctionTemplate申明的隱藏性(Visibility) 2005-12-17 15:08 清風(fēng)雨

            無(wú)意闖到這邊來(lái)了。

            我是在對(duì)幾個(gè)概念不太清楚的情況下,想找找說(shuō)明。本來(lái)是不管這些概念的,但,我想試試特化是不小心寫(xiě)錯(cuò)了,編譯器告訴我無(wú)法顯示化。
            我就納悶了,特化、偏特化、顯示化,所以,想找找概念的介紹,一不小心發(fā)現(xiàn)了這個(gè)blog站,而你說(shuō)的問(wèn)題,我又忍不住想開(kāi)一下口。^_^

            關(guān)于特化的匹配原則,就像我們期望的那樣,編譯器是優(yōu)選最接近的。
            最簡(jiǎn)單的測(cè)試方法,就是針對(duì)模板圓形,特化形式分別給出不同輸出,這樣,一眼便能知道編譯器選了誰(shuí)。  回復(fù)  更多評(píng)論   

            # re: 討論FunctionTemplate申明的隱藏性(Visibility) 2006-01-08 20:27 chenchen

            <c++ template>上說(shuō)必須把重載的寫(xiě)在template的前面,然而我在vs2003上的測(cè)試是無(wú)所謂,至于標(biāo)準(zhǔn)上怎么說(shuō)那我就不知道了,寫(xiě)在前面就一定保險(xiǎn),寫(xiě)在后面估計(jì)是有危險(xiǎn)!  回復(fù)  更多評(píng)論   

            # re: 討論FunctionTemplate申明的隱藏性(Visibility) 2007-05-27 23:37 xiongx

            這玩意跟C的先申明后使用是一脈相承的吧,template的很多編譯策略都是看起來(lái)能用就行,不會(huì)顧及全面的  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            色88久久久久高潮综合影院| 欧洲性大片xxxxx久久久| 久久久久久久久无码精品亚洲日韩 | 97久久天天综合色天天综合色hd| 久久久久亚洲av无码专区| 国产午夜福利精品久久2021 | 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 亚洲中文字幕久久精品无码APP| 久久久国产精品亚洲一区| 久久精品国产亚洲Aⅴ香蕉| 日本五月天婷久久网站| AA级片免费看视频久久| 99精品国产99久久久久久97| 国产真实乱对白精彩久久| 久久久一本精品99久久精品88 | 久久精品无码专区免费东京热| 国产精品青草久久久久福利99| 国产A三级久久精品| 久久久久国产视频电影| 青青草国产精品久久久久| 少妇久久久久久被弄高潮| 伊人久久大香线蕉无码麻豆 | 久久精品成人免费观看97| 色综合久久无码五十路人妻| 久久综合精品国产一区二区三区| 72种姿势欧美久久久久大黄蕉| 亚洲午夜久久久久久噜噜噜| 亚洲人成网站999久久久综合| 狠狠精品干练久久久无码中文字幕 | 国产精品久久久久aaaa| 亚洲色欲久久久综合网| yy6080久久| 亚洲乱码精品久久久久..| 久久人人爽人人爽人人片AV高清 | 婷婷综合久久中文字幕蜜桃三电影| 欧美亚洲另类久久综合婷婷| 久久久精品国产亚洲成人满18免费网站 | 欧美激情精品久久久久| 日韩亚洲欧美久久久www综合网| 狠狠干狠狠久久| 51久久夜色精品国产|