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

            羅朝輝(飄飄白云)

            關(guān)注嵌入式操作系統(tǒng),移動(dòng)平臺(tái),圖形開發(fā)。-->加微博 ^_^

              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              85 隨筆 :: 0 文章 :: 169 評(píng)論 :: 0 Trackbacks

            讓gcc支持成員函數(shù)模板的trick

            羅朝輝 (http://m.shnenglu.com/kesalin/)

            本文遵循“署名-非商業(yè)用途-保持一致”創(chuàng)作公用協(xié)議
             
            gcc 4.7.3 不支持成員函數(shù)模板特化。如下代碼:

            #ifndef __MEMFUNTEMPLATE_H__
            #define __MEMFUNTEMPLATE_H__

            #include <stdio.h>

            class Base {};
            class Derived : public Base {};

            struct Functor {
                template <typename T> void function() {
                    printf(" Primary template.\n");
                }

                template<>
                void function<int>(){
                    printf(" Specialization for int.\n");
                }

                template<> void function<Base *>() {
                    printf(" Specialization for Base *.\n");
                }
            };

            class Tester {
            public:
                static void DoTest()
                {
                    Functor functor;
                    functor.function<char>();
                    functor.function<int>();
                    functor.function<Base *>();
                    functor.function<Derived *>();
                }
            };

            #endif // __MEMFUNTEMPLATE_H__

            在 VS2010 中編譯運(yùn)行是沒(méi)有問(wèn)題的,但在 gcc 4.7.3下,編譯都通不過(guò):

            ../src/MemFunTemplate.h:21:14: error: <strong>explicit specialization in non-namespace scope</strong> ‘struct Functor’
            ../src/MemFunTemplate.h:22:24: error: template-id ‘function<int>’ in declaration of primary template
            ../src/MemFunTemplate.h:26:14: error: explicit specialization in non-namespace scope ‘struct Functor’
            ../src/MemFunTemplate.h:26:38: error: template-id ‘function<Base*>’ in declaration of primary template
            ../src/MemFunTemplate.h:26:21: error: ‘void Functor::function()’ cannot be overloaded
            ../src/MemFunTemplate.h:22:10: error: with ‘void Functor::function()’
            ../src/MemFunTemplate.cpp: In function ‘int main()’:
            ../src/MemFunTemplate.cpp:17:2: error: ‘DoTest’ is not a member of ‘Functor’


            為了達(dá)到近似成員函數(shù)模板特化的效果,可以利用成員函數(shù)主模板以及重載函數(shù)來(lái)實(shí)現(xiàn):


            /*
             * MemFunTemplate.h
             *
             *  Created on: Jul 12, 2013
             *      Author: http://blog.csdn.net/kesalin/
             */

            #ifndef MEMFUNTEMPLATE_H_
            #define MEMFUNTEMPLATE_H_
            #include <stdio.h>
            template<typename T>
            struct DummyIdentity {
                typedef T type;
            };
            class Base {};
            class Derived : public Base {};
            struct Functor {
                template <typename T> void function() {
                    function(DummyIdentity<T>());
                }
            private:
                template <typename T>
                void function(DummyIdentity<T>) {
                    printf(" Primary template DummyIdentity<T>.\n");
                }
                void function(DummyIdentity<int>) {
                    printf(" overload function for DummyIdentity<int>.\n");
                }
                void function(DummyIdentity<Base *>) {
                    printf(" overload function for DummyIdentity<Base *>.\n");
                }
            };
            class Tester {
            public:
                static void DoTest()
                {
                    Functor functor;
                    functor.function<char>();
                    functor.function<int>();
                    functor.function<Base *>();
                    functor.function<Derived *>();
                }
            };
            #endif /* MEMFUNTEMPLATE_H_ */


            調(diào)用 DoTest() 運(yùn)行結(jié)果如下:

             Primary template DummyIdentity<T>.
             overload function for DummyIdentity<int>.
             overload function for DummyIdentity<Base *>.
             Primary template DummyIdentity<T>.


            注意:

            VS2010 版本的代碼,模板形參為 T,在實(shí)例化不會(huì)進(jìn)行隱式類型轉(zhuǎn)換。即用 Derived * 當(dāng)作實(shí)參調(diào)用的是主模板,而不是 Base * 特化版本

            而在 gcc  下,模板形參雖然也為T,但影響重載決議的 function 參數(shù)為:DummyIdentity<T>,用不同的實(shí)際參數(shù)實(shí)例化該模板,得到的是一堆重載函數(shù)。因此用 Derived * 當(dāng)作實(shí)參時(shí),調(diào)用的函數(shù)自然就是實(shí)例化的 void function(DummyIdentity<T>)了。

             
            posted on 2013-07-12 22:04 羅朝輝 閱讀(6330) 評(píng)論(3)  編輯 收藏 引用 所屬分類: C/C++

            評(píng)論

            # re: 讓gcc支持成員函數(shù)模板的trick 2013-07-13 20:54 Marvin
            從objective-c轉(zhuǎn)到c++了?  回復(fù)  更多評(píng)論
              

            # re: 讓gcc支持成員函數(shù)模板的trick 2013-07-14 21:37 羅朝輝
            @Marvin
            哈哈,本就是從C++起家的~~  回復(fù)  更多評(píng)論
              

            # re: 讓gcc支持成員函數(shù)模板的trick 2014-08-15 21:28 acmol
            其實(shí)只需要
            a->template b()就可以調(diào)用了。。
            雖然我也不知道原因。  回復(fù)  更多評(píng)論
              

            亚洲国产精品无码成人片久久| 久久99精品久久久久久9蜜桃| 国内精品久久久久久麻豆| 亚洲AⅤ优女AV综合久久久| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久男人AV资源网站| 国产精品久久一区二区三区| 久久精品人人槡人妻人人玩AV| 久久综合视频网| 久久99热精品| 久久国产精品一国产精品金尊| 久久无码精品一区二区三区| 77777亚洲午夜久久多喷| 国产精品久久久久久久久久影院| 亚洲精品高清国产一久久| 无码人妻久久一区二区三区蜜桃 | 久久免费精品视频| 99久久精品国产一区二区| 久久99精品久久久久久齐齐 | 国产精品久久久久久久app| 久久精品亚洲男人的天堂| 九九久久99综合一区二区| 中文字幕人妻色偷偷久久| 亚洲国产成人久久综合碰| 国产免费福利体检区久久| 久久精品成人| 欧美精品久久久久久久自慰| av无码久久久久久不卡网站| 国产精品无码久久四虎| 亚洲国产视频久久| 99国产欧美久久久精品蜜芽| 久久91精品国产91久久麻豆| 亚洲午夜久久久精品影院| 伊人久久大香线蕉av一区| 狠狠狠色丁香婷婷综合久久五月| 国产精品内射久久久久欢欢| 亚洲精品tv久久久久久久久 | 久久国产精品免费| 色综合久久中文字幕无码| 精品久久久久久国产三级| 人妻精品久久久久中文字幕一冢本|