• <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ū)別重要在:
            一個是: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); 
            }                              

            一個是: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;
            }

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


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

            評論

            # 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的函數應該被匹配調用,跟聲明的前后沒關系

              回復  更多評論   

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

            上面是我讀一本書測試過的例子,書中曾說: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:,這里的意思比較適合第二種情況,即”一個是:inline int const& max申明在template <typename T>
            inline T const& max之后。”,我沒有能在其他C++編譯器上測試過,不知道C++標準中是否規(guī)定FunctionTemplate Resolution Rules,如果沒有,依據上面的英文描述,不同編譯器有不同的FunctionTemplate Resolution Rule,有可能template <typename T>
            inline T const& max (T const& a, T const& b, T const& c) 不可見到申明在后的inline int const& max。

            大家的意見呢?  回復  更多評論   

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

            無意闖到這邊來了。

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

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

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

            <c++ template>上說必須把重載的寫在template的前面,然而我在vs2003上的測試是無所謂,至于標準上怎么說那我就不知道了,寫在前面就一定保險,寫在后面估計是有危險!  回復  更多評論   

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

            這玩意跟C的先申明后使用是一脈相承的吧,template的很多編譯策略都是看起來能用就行,不會顧及全面的  回復  更多評論   

            色偷偷91久久综合噜噜噜噜| 久久免费国产精品一区二区| 亚洲国产婷婷香蕉久久久久久| 亚洲午夜精品久久久久久app| 77777亚洲午夜久久多人| 丁香五月网久久综合| 欧美一级久久久久久久大片| 亚洲精品国产字幕久久不卡| 四虎国产精品免费久久5151| 91精品婷婷国产综合久久| 无码人妻少妇久久中文字幕 | 国产成人久久777777| 久久久久久久久66精品片| 久久无码高潮喷水| 亚洲国产精品久久久久婷婷老年| A级毛片无码久久精品免费| 1000部精品久久久久久久久| 香蕉久久AⅤ一区二区三区| 国产精品久久久久久久久久影院| 欧美午夜精品久久久久久浪潮| av无码久久久久久不卡网站| 久久久久一本毛久久久| www久久久天天com| 亚洲精品tv久久久久久久久 | 久久久亚洲欧洲日产国码二区| 久久久久国产亚洲AV麻豆| 72种姿势欧美久久久久大黄蕉| 久久亚洲精品无码观看不卡| 国产亚洲婷婷香蕉久久精品| 久久亚洲精品人成综合网| 亚洲中文久久精品无码ww16 | 久久久这里有精品| 麻豆国内精品久久久久久| 久久精品国产亚洲欧美| 嫩草影院久久国产精品| 久久青草国产手机看片福利盒子| 99久久中文字幕| 精品国产婷婷久久久| 国内精品久久久久久久久电影网| 日本久久久精品中文字幕| 国产高清美女一级a毛片久久w|