• <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>
            Cpper
            C/C++高級(jí)工程師 Android高級(jí)軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿

            以前大致看過模板元編程但是并沒有深入進(jìn)去
            現(xiàn)在就拿比較小的Loki庫研究了
            本文主要涉及的是其頭文件TypeMapIP.h
            1.根據(jù)給定常量生成對(duì)等枚舉變量

            ////////////////////////////////////////////////////////////////////////////////
            // class template Int2Type
            // Converts each integral constant into a unique type
            // Invocation: Int2Type<v> where v is a compile-time constant integral
            // Defines 'value', an enum that evaluates to v
            ////////////////////////////////////////////////////////////////////////////////

                template 
            <int v>
                
            struct Int2Type
                {
                    
            enum { value = v };
                };
            正如所說:v是一個(gè)編譯期常量整數(shù)
            2.類型重定義
            ////////////////////////////////////////////////////////////////////////////////
            // class template Type2Type
            // Converts each type into a unique, insipid type
            // Invocation Type2Type<T> where T is a type
            // Defines the type OriginalType which maps back to T
            ////////////////////////////////////////////////////////////////////////////////

                template 
            <typename T>
                
            struct Type2Type
                {
                    typedef T OriginalType;
                };
            舉例子為:
            Type2Type<vector<int> >::OriginalType v;
            當(dāng)前蓋莫引擎就是這樣使用鏈表的O(∩_∩)O~如下:
                #include <loki/flex/flex_string.h>
                typedef flex_string
            <char> engine_string;
                #include 
            <loki/yasli/yasli_vector.h>     
                template
            <class T>      
                
            struct vector_typedef
                {
                    typedef yasli::vector
            <T,std::allocator<T> > type;
                };   
                template
            <class T>      
                
            struct list_typedef
                {
                    typedef yasli::vector
            <T,std::allocator<T> > type;       
                };        
            #endif 
            基本上是模板重定義
            3.條件類型重定義
            ////////////////////////////////////////////////////////////////////////////////
            // class template Select
            // Selects one of two types based upon a boolean constant
            // Invocation: Select<flag, T, U>::Result
            // where:
            // flag is a compile-time boolean constant
            // T and U are types
            // Result evaluates to T if flag is true, and to U otherwise.
            ////////////////////////////////////////////////////////////////////////////////

                template 
            <bool flag, typename T, typename U>
                
            struct Select
                {
                    typedef T Result;
                };
                template 
            <typename T, typename U>
                
            struct Select<false, T, U>
                {
                    typedef U Result;
                };
            舉例為:
            Select<true,int,char>::Result r;
            則r為int 類型
            Seclet<false,int,char>::Result r
            r為char類型
            這樣的例子基本上在所有模板元編程書上都可以看到
            當(dāng)然還有很多代碼,比如DyObjLib庫
            4.檢測對(duì)象是否為同一類型
                template <typename T, typename U>
                
            struct IsSameType
                {
                    
            enum { value = false };
                };
                
                template 
            <typename T>
                
            struct IsSameType<T,T>
                {
                    
            enum { value = true };
                };
            記得以前我看到這個(gè)的時(shí)候感覺實(shí)現(xiàn)的太巧妙了
            5.Conversion模板
            ////////////////////////////////////////////////////////////////////////////////
            // class template Conversion
            // Figures out the conversion relationships between two types
            // Invocations (T and U are types):
            // a) Conversion<T, U>::exists
            // returns (at compile time) true if there is an implicit conversion from T
            // to U (example: Derived to Base)
            // b) Conversion<T, U>::exists2Way
            // returns (at compile time) true if there are both conversions from T
            // to U and from U to T (example: int to char and back)
            // c) Conversion<T, U>::sameType
            // returns (at compile time) true if T and U represent the same type
            //
            // Caveat: might not work if T and U are in a private inheritance hierarchy.
            ////////////////////////////////////////////////////////////////////////////////
            Conversion模板指出了給定2個(gè)類型之間的轉(zhuǎn)換關(guān)系
            具體源碼為:
                template <class T, class U>
                
            struct Conversion
                {
                    typedef Private::ConversionHelper
            <T, U> H;
            #ifndef __MWERKS__
                    
            enum { exists = sizeof(typename H::Small) == sizeof((H::Test(H::MakeT()))) };
            #else
                    
            enum { exists = false };
            #endif
                    
            enum { exists2Way = exists && Conversion<U, T>::exists };
                    
            enum { sameType = false };
                };
            _MWERKS是什么宏定義
            先不管它
            再看其特化形式
                template <class T>
                
            struct Conversion<T, T>    
                {
                    
            enum { exists = 1, exists2Way = 1, sameType = 1 };
                };
                
                template 
            <class T>
                
            struct Conversion<void, T>    
                {
                    
            enum { exists = 0, exists2Way = 0, sameType = 0 };
                };
                
                template 
            <class T>
                
            struct Conversion<T, void>    
                {
                    
            enum { exists = 0, exists2Way = 0, sameType = 0 };
                };
                
                template 
            <>
                
            struct Conversion<voidvoid>    
                {
                
            public:
                    
            enum { exists = 1, exists2Way = 1, sameType = 1 };
                };

            這里給定了4種特化形式
            3個(gè)涉及void類型
            1個(gè)涉及類型相同
            可以看出:
            如果2個(gè)對(duì)象類型相同則枚舉變量sameType必定為真
            關(guān)于枚舉變量exists
            如果其為真則說明T類型可以轉(zhuǎn)化為U類型(比如子類到父類)
            關(guān)于變量exists2Way表達(dá)的是U,T類型是否可以雙向轉(zhuǎn)化(比如int,和long類型)
            6.繼承關(guān)系的鑒別
            ////////////////////////////////////////////////////////////////////////////////
            // class template SuperSubclass
            // Invocation: SuperSubclass<B, D>::value where B and D are types. 
            // Returns true if B is a public base of D, or if B and D are aliases of the 
            // same type.
            //
            // Caveat: might not work if T and U are in a private inheritance hierarchy.
            ////////////////////////////////////////////////////////////////////////////////
            如果B類型是D類型的公共父類則結(jié)果為真
            否則為假
            代碼為:
            template <class T, class U>
            struct SuperSubclass
            {
                
            enum { value = (::Loki::Conversion<const volatile U*const volatile T*>::exists &&
                              
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
                  
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == sizeof (U) ) };
            };

            template 
            <>
            struct SuperSubclass<voidvoid> 
            {
                
            enum { value = false };
            };

            template 
            <class U>
            struct SuperSubclass<void, U> 
            {
                
            enum { value = (::Loki::Conversion<const volatile U*const volatile void*>::exists &&
                              
            !::Loki::Conversion<const volatile void*const volatile void*>::sameType) };
                  
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
            };

            template 
            <class T>
            struct SuperSubclass<T, void> 
            {
                
            enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                              
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
                  
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
            };
            其幾種特化形式都與void類型有關(guān)
            再看其value描述:
                enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                              
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
            當(dāng)void*可以轉(zhuǎn)化為T*同時(shí)T*類型不為void*時(shí)則value為真
            enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
            該枚舉變量時(shí)防止類型變量存在不完全類型
            可以看出在模板元編程中可以使用enum變量來處理和預(yù)報(bào)錯(cuò)誤
            給定一個(gè)例子
            #include <iostream>
            #include 
            <string>
            #include 
            <Loki/TypeManIP.h>
            #include 
            <typeinfo>

            class A; 
             
            int main()
            {
                std::cout
            <<Loki::SuperSubclass<void,A>::dontUseWithIncompleteTypes<<std::endl; 
                system(
            "PAUSE");
                
            return EXIT_SUCCESS;
            }
            編譯器會(huì)報(bào)出一個(gè)194 E:\c++header\Loki\TypeManIP.h invalid application of `sizeof' to incomplete type `A'  錯(cuò)誤
            7.對(duì)象嚴(yán)格繼承關(guān)系的鑒別
            ////////////////////////////////////////////////////////////////////////////////
            // class template SuperSubclassStrict
            // Invocation: SuperSubclassStrict<B, D>::value where B and D are types. 
            // Returns true if B is a public base of D.
            //
            // Caveat: might not work if T and U are in a private inheritance hierarchy.
            ////////////////////////////////////////////////////////////////////////////////

            template
            <class T,class U>
            struct SuperSubclassStrict
            {
                
            enum { value = (::Loki::Conversion<const volatile U*const volatile T*>::exists &&
                             
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType &&
                             
            !::Loki::Conversion<const volatile T*const volatile U*>::sameType) };
                
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == sizeof (U) ) };
            };

            template
            <>
            struct SuperSubclassStrict<voidvoid> 
            {
                
            enum { value = false };
            };

            template
            <class U>
            struct SuperSubclassStrict<void, U> 
            {
                
            enum { value = (::Loki::Conversion<const volatile U*const volatile void*>::exists &&
                             
            !::Loki::Conversion<const volatile void*const volatile void*>::sameType &&
                             
            !::Loki::Conversion<const volatile void*const volatile U*>::sameType) };
                
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( 0 == sizeof (U) ) };
            };

            template
            <class T>
            struct SuperSubclassStrict<T, void> 
            {
                
            enum { value = (::Loki::Conversion<const volatile void*const volatile T*>::exists &&
                             
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType &&
                             
            !::Loki::Conversion<const volatile T*const volatile void*>::sameType) };
                
                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
            };
            以上是代碼段
            當(dāng)然以上2種類型檢測都不能處理私有繼承的關(guān)系
            下面是簡單的測試?yán)?

            #include <iostream>
            #include 
            <string>
            #include 
            <Loki/TypeManIP.h>
            #include 
            <typeinfo>

            class B
            {}; 

            class D : public B
            {
            };
             
            int main()
            {
                std::cout
            <<LOKI_SUPERSUBCLASS(B,D)<<std::endl; 
                std::cout
            <<LOKI_SUPERSUBCLASS(D,B)<<std::endl; 
                std::cout
            <<LOKI_SUPERSUBCLASS(B,int)<<std::endl; 
                std::cout
            <<LOKI_SUPERSUBCLASS(long,D)<<std::endl; 
                system(
            "PAUSE");
                
            return EXIT_SUCCESS;
            }
            TypemanIp看完了
            8.最后我給出我看到的另外一種檢測對(duì)象是否含有Vtable的檢測手法:
            template<class T>
            struct IsVObjImpl {
                
            struct IA : public T {
                    
            int m;
                };
                
            struct IB : public T {
                    
            virtual void F( );
                    
            int m;
                };
                
            enum { v = (sizeof(IA)==sizeof(IB)) };
            };
            首先定義2個(gè)類對(duì)象他們都繼承于給定類型
            然后一個(gè)還有Vtable
            一個(gè)不含有Vtable
            之后偵測其size是否相同
            可以看出vtable是不占有子類大小的
            測試?yán)尤缦?
            #include <iostream>
             
            template
            <class T>
            struct IsVObjImpl 
            {
                
            struct IA : public T {
                    
            int m;
                };
                
            struct IB : public T {
                    
            virtual void F( );
                    
            int m;
                };
                
            enum { v = (sizeof(IA)==sizeof(IB)) };
            };
             
            class A
            {
               
            virtual void v(){}      
            }; 

            class B
            {
               
            void v(){}      
            }; 

             
            int main()
            {
                std::cout
            <<IsVObjImpl<A>::v<<std::endl;
                std::cout
            <<IsVObjImpl<B>::v<<std::endl;
                system(
            "PAUSE");
                
            return EXIT_SUCCESS;
            }

            9.一些其他的對(duì)象類型測試手法:
            // Check if type is const
            template <class T>
            struct DoIsConst {
                
            enum { v=0 };
            };

            template 
            <class T>
            struct DoIsConst<const T> {
                
            enum { v=1 };
            };

            template 
            <class T>  // Should this be needed?
            struct DoIsConst<const T&> {
                
            enum { v=1 };
            };

            template 
            <class T>  // Should this be needed?
            struct DoIsConst<const T*> {
                
            enum { v=1 };
            };


            /////////////////////////////////////////
            // Check if type is a ref or not
            template <class T>
            struct DoIsRef {
                
            enum { v=0 };
            };

            template 
            <class T>
            struct DoIsRef<T&> {
                
            enum { v=1 };
            };


            /////////////////////////////////////////
            // Check if type is a pointer or not
            template <class T>
            struct DoIsPointer {
                
            enum { v=0 };
            };

            template 
            <class T>
            struct DoIsPointer<T*> {
                
            enum { v=1 };
            };
            這些東西都源于DyObj
            A C++ framework for binary reusable objects, or plugins. It enables exposing and sharing run-time type information for C++ classes
            當(dāng)然boost中可定可以找到類型代碼的
            應(yīng)該在type_traits中吧?
            posted on 2010-04-18 18:28 ccsdu2009 閱讀(2072) 評(píng)論(6)  編輯 收藏 引用
            Comments
            • # re: Loki技法5-TypeMap
              luoweisong
              Posted @ 2010-04-19 10:58
              很想認(rèn)真看你的文章,但你的頁面顏色大讓人不舒服了  回復(fù)  更多評(píng)論   
            • # re: Loki技法5-TypeMap
              空明流轉(zhuǎn)
              Posted @ 2010-04-19 11:49
              traits類似于不動(dòng)點(diǎn),模板類似于lambda算子。整個(gè)metaprogramming體系就是一個(gè)template functional language。

              boost.mpl+traits才是正道。  回復(fù)  更多評(píng)論   
            • # re: Loki技法5-TypeMap
              ccsdu2009
              Posted @ 2010-04-19 11:58
              @luoweisong
              我不太懂如何配置顏色
              有空你可以教教我如何設(shè)置
                回復(fù)  更多評(píng)論   
            • # re: Loki技法5-TypeMap
              ccsdu2009
              Posted @ 2010-04-19 11:59
              @空明流轉(zhuǎn)
              我現(xiàn)在已經(jīng)不大使用boost了
              這個(gè)東西大的出奇
              (當(dāng)然我只在底層使用若干子庫)  回復(fù)  更多評(píng)論   
            • # re: Loki技法5-TypeMap
              空明流轉(zhuǎn)
              Posted @ 2010-04-19 12:40
              我是說在methodology上。
              至于大不大,我覺得至少boost比loki好,后者幾乎是一個(gè)實(shí)驗(yàn)性質(zhì)的東西。  回復(fù)  更多評(píng)論   
            • # re: Loki技法5-TypeMap
              空明流轉(zhuǎn)
              Posted @ 2010-04-19 12:43
              還有,你換一個(gè)template吧。你這個(gè)template的顏色實(shí)在難看的不行。  回復(fù)  更多評(píng)論   

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


             
            国产激情久久久久影院小草| 久久人人爽人人澡人人高潮AV | 国产精品久久久久一区二区三区| 国产成人久久精品一区二区三区| 久久天堂电影网| 欧美久久综合九色综合| 亚洲成色www久久网站夜月| 天天久久狠狠色综合| 中文字幕亚洲综合久久菠萝蜜| 伊人久久大香线蕉亚洲五月天| 久久精品国产秦先生| 中文字幕乱码人妻无码久久| 婷婷综合久久狠狠色99h| 色老头网站久久网| 国产精品激情综合久久| 久久无码人妻一区二区三区午夜| 国产精品热久久无码av| 久久w5ww成w人免费| 精品久久亚洲中文无码| 久久精品无码av| 97久久超碰成人精品网站| 亚洲中文精品久久久久久不卡| 成人精品一区二区久久| 国产美女久久精品香蕉69| 97久久国产综合精品女不卡| 狠狠精品干练久久久无码中文字幕| 色偷偷偷久久伊人大杳蕉| 久久精品国产亚洲αv忘忧草 | 久久久久亚洲AV成人片| 日韩va亚洲va欧美va久久| 国产精品无码久久综合网| 精品久久久久久久| 久久亚洲欧美国产精品| 无码国内精品久久人妻蜜桃 | 超级碰久久免费公开视频| 国产精品视频久久久| 69久久精品无码一区二区| 免费观看久久精彩视频| 国产成人精品久久综合| 国内精品免费久久影院| 中文字幕精品久久|