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

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            C++模版:編譯期檢測可轉(zhuǎn)換和可繼承

            《C++設(shè)計新思維》
            雖然可能實際中可能用不著,但是可以作為理解模版的特化哦!

            //C++模版:編譯期檢測可轉(zhuǎn)換和可繼承
            //一般我們使用dynamic_cast<>在運行期進(jìn)行轉(zhuǎn)化,但是對于模版編程,我們可以實現(xiàn)編譯時類型檢測。利用了模版技術(shù),sizeof的編譯期就可以得到結(jié)果和函數(shù)的重載技術(shù)。

            //比如我們檢測T是否可以轉(zhuǎn)化為U,我們實現(xiàn)一個函數(shù)Test和他的一個重載,函數(shù)的參數(shù)分別是U和。。。
            //參數(shù)U和。。。分別對應(yīng)可以轉(zhuǎn)化為U和不可以轉(zhuǎn)化的情況,然后我們通過對2個重載函數(shù)給于不同的返回值,在編譯時用sizeof取得不同返回值的大小來判斷是否可以轉(zhuǎn)化。

            //Determine whether the two types are the same type
            template<class T, class U>
            struct IsSameType
            {
                
            enum { value = false };
            }
            ;

            template
            <class T>
            struct IsSameType<T, T>
            {
                
            enum { value = true };
            }
            ;

            //Helper that help to determine whether type T can convert to type U
            template <class T, class U>
            struct ConversionHelper
            {
                typedef 
            char Small;
                
            struct Big char dummy[2]; };
                
            static Big   Test();
                
            static Small Test(U);
                
            static T MakeT();
            }
            ;

            //我們都知道在C++中,子類的指針可以轉(zhuǎn)化為基類的指針
            //判斷T是否能夠轉(zhuǎn)化為U類型 :T和U是否是同一類型或U是否是T的基類
            template<class T, class U>
            struct Conversion
            {
                typedef ConversionHelper
            <T, U> H;

                
            enum { exists = sizeof(typename H::Small) == sizeof((H::Test(H::MakeT()))) };

                
            enum { exists2Way = exists && Conversion<U, T>::exists };

                
            enum { sameType = false };
            }
            ;

            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 };
            }
            ;

            //上面BigType和SmallType表示返回結(jié)果,他們的sizeof必須不同。
            //MakeT()確保不管T的構(gòu)造函數(shù)的私有或共有都有一個零時的T供sizeof使用。

            //T是U的基類或T和U是同一類的2個別名。
            //注意SuperSubClass傳入的模版參數(shù)與內(nèi)部調(diào)用Conersion函數(shù)的參數(shù)傳入順序,在參數(shù)前面加了const volatile限定了不能使用重載的類型轉(zhuǎn)化函數(shù)。
            template <class T, class U>
            struct SuperSubclass
            {
                
            enum { value = (Conversion<const volatile U*const volatile T*>::exists &&
                    
            !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 = (Conversion<const volatile U*const volatile void*>::exists &&
                    
            !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 = (Conversion<const volatile void*const volatile T*>::exists &&
                    
            !Conversion<const volatile T*const volatile void*>::sameType) }
            ;

                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
            }
            ;




            //T是U的基類
            template<class T,class U>
            struct SuperSubclassStrict
            {
                
            enum { value = (Conversion<const volatile U*const volatile T*>::exists &&
                    
            !Conversion<const volatile T*const volatile void*>::sameType &&
                    
            !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 = (Conversion<const volatile U*const volatile void*>::exists &&
                    
            !Conversion<const volatile void*const volatile void*>::sameType &&
                    
            !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 = (Conversion<const volatile void*const volatile T*>::exists &&
                    
            !Conversion<const volatile T*const volatile void*>::sameType &&
                    
            !Conversion<const volatile T*const volatile void*>::sameType) }
            ;

                
            // Dummy enum to make sure that both classes are fully defined.
                enum{ dontUseWithIncompleteTypes = ( sizeof (T) == 0 ) };
            }
            ;

            //test classes
            class CBase
            {public:
            int m;
            }
            ;

            class CDerived : public CBase
            {
            public:
                
            int n;
            }
            ;

            class COther
            {
            public:
                
            int o;
            }
            ;

            class CConvertToBase
            {
            public:
                
            int p;
            public:
                
            operator CBase()
                
            {
                    CBase obj;
                    
            return obj;
                }

            }
            ;

            void main()
            {
                
            using namespace std;
                cout
            << Conversion<doubleint>::exists << ' '
                    
            << Conversion<intdouble>::exists2Way<<" "
                    
            << Conversion<intdouble>::sameType<<endl;

                cout
            << Conversion<charchar*>::exists << ' '
                    
            << Conversion<size_t, vector<int> >::exists <<endl;

                cout
            << "Conversion from CDerived to CBase : "<<Conversion<CDerived,CBase>::exists<<endl;
                    cout
            << "Conversion from CBase to CDerived : "<<Conversion<CBase,CDerived>::exists2Way<<endl;
                    cout
            <<"is CBase and CDerived the same type: "<<Conversion<CBase,CDerived>::sameType<<endl;

                cout
            << "conversion from CConvertToBase to CBase : "<<Conversion<CConvertToBase,CBase>::exists<<endl;
                cout
            << "Conversion from CConvertToBase to CDerived : "<<Conversion<CConvertToBase,CDerived>::exists<<endl<<endl;





                
            //Is T derived from U
                std::cout<< "CDerived is the super class or the same class of CBase: "<<SuperSubclass<CDerived, CBase>::value<<std::endl;
                std::cout 
            << "CBase is the super class or the same class of CDerived: " << SuperSubclass<CBase, CDerived>::value << std::endl;
                std::cout 
            << "COther is the super class or the same class of CBase:" << SuperSubclass<COther, CBase>::value << std::endl;
                std::cout 
            << "CBase is the super class or the same class of COther: " << SuperSubclass<CBase, COther>::value << std::endl;
                std::cout 
            << "CConvertToBase is the super class or the same class of CBase: " << SuperSubclass<CConvertToBase, CBase>::value << std::endl;
                std::cout 
            << "CBase is the super class or the same class of CConvertToBase: " << SuperSubclass<CBase, CConvertToBase>::value << std::endl;
                std::cout 
            << "void is the super class or the same class of CBase: " << SuperSubclass<void, CBase>::value << std::endl;
                std::cout 
            << "COther is the super class or the same class of void: " << SuperSubclass<COther, void>::value << std::endl;
                std::cout 
            << "void is the super class or the same class of void: " << SuperSubclass<voidvoid>::value << std::endl;
                std::cout 
            << "CBase is the super class or the same class of CBase: " << SuperSubclass<CBase, CBase>::value << std::endl;

                std::cout 
            << std::endl;

                
            //Is T derived from U, use strict version which exclude the same type
                std::cout << "CDerived is the super class of CBase : " << SuperSubclassStrict<CDerived, CBase>::value << std::endl;
                std::cout 
            << "CBase is the super class of CDerived :  : " << SuperSubclassStrict<CBase, CDerived>::value << std::endl;
                std::cout 
            << "COther is the super class of CBase : : " << SuperSubclassStrict<COther, CBase>::value << std::endl;
                std::cout 
            << "CBase is the super class of COther : : " << SuperSubclassStrict<CBase, COther>::value << std::endl;
                std::cout 
            << "CConvertToBase is the super class of CBase : : " << SuperSubclassStrict<CConvertToBase, CBase>::value << std::endl;
                std::cout 
            << "CBase is the super class of CConvertToBase : : " << SuperSubclassStrict<CBase, CConvertToBase>::value << std::endl;
                std::cout 
            << "void is the super class of CBase : : " << SuperSubclassStrict<void, CBase>::value << std::endl;
                std::cout 
            << "COther is the super class of void : : " << SuperSubclassStrict<COther, void>::value << std::endl;
                std::cout 
            << "void is the super class of void : : " << SuperSubclassStrict<voidvoid>::value << std::endl;
                std::cout 
            << "CBase is the super class of CBase : : " << SuperSubclassStrict<CBase, CBase>::value << std::endl;
            }

              張張見識哦!~~~

            posted on 2007-05-24 18:21 夢在天涯 閱讀(1963) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlus

            評論

            # re: C++模版:編譯期檢測可轉(zhuǎn)換和可繼承 2008-07-07 09:23 cexer

            《C++設(shè)計新思維》里都有詳細(xì)的原理闡述。boost里有很好的實現(xiàn)。  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1808189
            • 排名 - 5

            最新評論

            閱讀排行榜

            伊人丁香狠狠色综合久久| yellow中文字幕久久网| 亚洲人成网站999久久久综合 | 亚洲国产成人久久一区久久| 久久精品成人免费看| 久久久国产99久久国产一| 色欲av伊人久久大香线蕉影院 | 精品久久久久一区二区三区| 亚洲精品乱码久久久久久按摩| 亚洲中文久久精品无码| 国内精品久久久久久麻豆| 成人午夜精品无码区久久| 久久精品女人天堂AV麻| 91精品国产91久久久久久| 久久精品青青草原伊人| 狠狠色婷婷久久综合频道日韩 | 2021国内久久精品| 欧洲国产伦久久久久久久 | 亚洲人成无码网站久久99热国产| 久久久久女人精品毛片| 色综合久久综合中文综合网| 久久精品无码专区免费| 久久九九全国免费| 伊人久久综合热线大杳蕉下载| 亚洲AV成人无码久久精品老人 | 久久精品国产99久久丝袜| 99re久久精品国产首页2020| 久久不见久久见免费视频7| 婷婷国产天堂久久综合五月| 久久国产成人精品国产成人亚洲| 中文字幕亚洲综合久久| 伊人色综合久久天天| 亚洲国产精品久久久久婷婷老年| 国产亚洲欧美精品久久久| 亚洲综合久久综合激情久久| 97久久精品午夜一区二区| 久久精品嫩草影院| 91精品无码久久久久久五月天 | 久久99国产综合精品| 久久99精品久久久久婷婷| 久久国产亚洲精品无码|