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

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評(píng)論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

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

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

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

            //比如我們檢測(cè)T是否可以轉(zhuǎn)化為U,我們實(shí)現(xiàn)一個(gè)函數(shù)Test和他的一個(gè)重載,函數(shù)的參數(shù)分別是U和。。。
            //參數(shù)U和。。。分別對(duì)應(yīng)可以轉(zhuǎn)化為U和不可以轉(zhuǎn)化的情況,然后我們通過(guò)對(duì)2個(gè)重載函數(shù)給于不同的返回值,在編譯時(shí)用sizeof取得不同返回值的大小來(lái)判斷是否可以轉(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<void, void>   
            {
            public:
               
            enum { exists = 1, exists2Way = 1, sameType = 1 };
            }
            ;

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

            //T是U的基類或T和U是同一類的2個(gè)別名。
            //注意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<void, void> 
            {
               
            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<void, void> 
            {
               
            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<double, int>::exists << ' '
                   
            << Conversion<int, double>::exists2Way<<" "
                   
            << Conversion<int, double>::sameType<<endl;

                cout
            << Conversion<char, char*>::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<void, void>::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<void, void>::value << std::endl;
                std::cout
            << "CBase is the super class of CBase : : " << SuperSubclassStrict<CBase, CBase>::value << std::endl;
            }

              張張見識(shí)哦!~~~

            posted on 2008-09-14 18:02 肥仔 閱讀(423) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            激情伊人五月天久久综合| 久久精品中文字幕一区| 国内精品久久久久久久久电影网| 激情五月综合综合久久69| 色综合久久天天综线观看| 一本色道久久综合亚洲精品| 狠狠88综合久久久久综合网| 国产成人AV综合久久| 日韩欧美亚洲综合久久 | 久久青青色综合| 亚洲综合熟女久久久30p| 91久久精品91久久性色| 无码国内精品久久人妻麻豆按摩 | 精品国产乱码久久久久软件| 噜噜噜色噜噜噜久久| 国内精品伊人久久久久av一坑| 久久精品中文字幕一区| 久久九九精品99国产精品| 久久99精品久久久久久噜噜| 亚洲AV无码久久精品色欲| 亚洲狠狠久久综合一区77777| 99久久这里只精品国产免费| 久久精品这里热有精品| 亚洲国产美女精品久久久久∴| 久久成人精品| 青青青国产精品国产精品久久久久| 久久精品国产2020| 亚洲AV伊人久久青青草原| 亚洲国产精品久久久久婷婷老年| 久久精品国产乱子伦| 亚洲国产小视频精品久久久三级| 97久久久久人妻精品专区| 久久人人爽人人爽人人片AV不 | 久久久久亚洲精品中文字幕| 国产亚洲欧美成人久久片| 一本一道久久综合狠狠老| 亚洲国产成人精品91久久久 | 久久天天躁狠狠躁夜夜躁2014| 久久精品国产第一区二区| 91麻精品国产91久久久久| 久久精品亚洲精品国产色婷|