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

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            C++模版:編譯期檢測可轉換和可繼承

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

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

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

            //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++中,子類的指針可以轉化為基類的指針
            //判斷T是否能夠轉化為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表示返回結果,他們的sizeof必須不同。
            //MakeT()確保不管T的構造函數的私有或共有都有一個零時的T供sizeof使用。

            //T是U的基類或T和U是同一類的2個別名。
            //注意SuperSubClass傳入的模版參數與內部調用Conersion函數的參數傳入順序,在參數前面加了const volatile限定了不能使用重載的類型轉化函數。
            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;
            }

              張張見識哦!~~~

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

            国内精品久久久久影院薰衣草 | 亚洲精品tv久久久久久久久久| 久久精品亚洲男人的天堂| 色狠狠久久综合网| 久久精品亚洲日本波多野结衣| 91超碰碰碰碰久久久久久综合| 人妻无码久久精品| 精品久久久久久国产91| 青青青青久久精品国产h久久精品五福影院1421| 久久丫忘忧草产品| 日本精品久久久久中文字幕8| 久久精品国产清自在天天线| 2021国产成人精品久久| 影音先锋女人AV鲁色资源网久久| 国内精品久久久久久久coent| 久久精品国产亚洲精品2020| 色老头网站久久网| 精品综合久久久久久88小说| 国产Av激情久久无码天堂| 2020久久精品亚洲热综合一本| 国产精品成人久久久久久久| 狠狠干狠狠久久| 久久久久亚洲AV成人片 | 狠狠色丁香婷婷综合久久来| 一本一本久久a久久综合精品蜜桃| 国产精品美女久久久久av爽 | 久久午夜夜伦鲁鲁片免费无码影视| 亚洲国产精品久久久久| 91精品国产高清久久久久久io| 亚洲精品乱码久久久久久蜜桃不卡| 无码任你躁久久久久久久| 久久WWW免费人成—看片| 久久精品一区二区国产| 2021少妇久久久久久久久久| 久久无码人妻一区二区三区午夜 | 无码任你躁久久久久久| 久久婷婷五月综合97色直播| 四虎亚洲国产成人久久精品| 亚洲人成无码网站久久99热国产| 亚洲欧美成人久久综合中文网 | 国产99久久久久久免费看|