青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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 肥仔 閱讀(428) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲国产美女精品久久久久∴| 欧美一二三视频| 久久成人精品| 久久综合色88| 亚洲第一主播视频| 亚洲激情六月丁香| 亚洲精品1234| 欧美在线视频二区| 欧美午夜性色大片在线观看| 国产日韩欧美麻豆| 亚洲精品一区二区三区樱花| 欧美一级视频一区二区| 亚洲第一区在线观看| 欧美久久精品午夜青青大伊人| 午夜欧美电影在线观看| 麻豆久久久9性大片| 国产欧美精品日韩| 亚洲午夜免费福利视频| 美日韩精品视频| 午夜欧美精品久久久久久久| 在线高清一区| 久久国产精品72免费观看| 久久五月婷婷丁香社区| 国产小视频国产精品| 亚洲国产一区二区三区a毛片| 欧美激情精品久久久久久免费印度| 性欧美video另类hd性玩具| 亚洲天堂av电影| 欧美日韩国产成人高清视频| 亚洲国产视频直播| 亚洲一区二区三区四区在线观看| 香蕉久久精品日日躁夜夜躁| 91久久精品美女高潮| 麻豆成人av| 国产精品看片你懂得| 中文成人激情娱乐网| 亚洲精品视频二区| 欧美韩日一区二区| 亚洲理论在线观看| 亚洲人体影院| 欧美视频一区二区在线观看| 亚洲视频欧美视频| 免费欧美网站| 亚洲午夜精品福利| 欧美成人精品一区二区三区| 亚洲人成在线观看| 久久激情五月丁香伊人| 亚洲国产第一| 亚洲精品美女在线观看| 欲色影视综合吧| 久久国产精品久久久久久电车| 国产日韩欧美高清| 一区二区三区免费观看| 国产精品入口夜色视频大尺度| 国产综合色产| 麻豆精品视频在线观看视频| 国产精品久久久久久久久久久久 | 欧美黄色影院| 国产在线视频不卡二| 宅男噜噜噜66一区二区| 国产一区二区久久精品| 欧美成人蜜桃| 欧美性感一类影片在线播放| 日韩午夜在线视频| 国产一区二区三区久久悠悠色av | 亚洲国产精品美女| 久久综合九色综合网站| 一本一本久久a久久精品牛牛影视| 久久福利一区| 国产精品一区二区久久精品| 美国成人直播| 在线播放国产一区中文字幕剧情欧美 | 国产欧美日韩综合一区在线观看| 欧美一区二区日韩| 久热精品在线视频| 欧美激情欧美狂野欧美精品| 亚洲国内自拍| 欧美日韩1234| 亚洲曰本av电影| 久久香蕉国产线看观看网| 欧美日韩福利| 亚洲尤物在线视频观看| 欧美在线观看www| 精品999久久久| 亚洲欧美日韩国产一区| 99re8这里有精品热视频免费| 欧美激情在线| 夜夜狂射影院欧美极品| 欧美午夜三级| 久久久99精品免费观看不卡| 亚洲欧美国产毛片在线| 久久综合99re88久久爱| 亚洲国产日韩欧美在线图片| 亚洲午夜在线视频| 黄色一区二区在线| 欧美日韩精品中文字幕| 欧美在线www| 亚洲精品在线免费| 久久综合99re88久久爱| 亚洲一级电影| 亚洲高清视频的网址| 国产精品成人免费视频 | 欧美大片va欧美在线播放| 一卡二卡3卡四卡高清精品视频| 久久国产一区| 亚洲三级视频| 免费欧美网站| 欧美在线视频一区二区三区| 日韩午夜高潮| 激情综合网址| 国产免费一区二区三区香蕉精| 日韩视频在线观看免费| 老牛影视一区二区三区| 亚洲综合激情| 国产精品久久久一区二区三区| 欧美激情国产日韩精品一区18| 国产综合18久久久久久| 欧美日韩精品二区| 美国成人直播| 久久成人人人人精品欧| 亚洲综合色网站| 久久人人97超碰精品888| 国内精品久久久久久影视8| 欧美日韩在线视频观看| 欧美成人午夜77777| 久久久噜噜噜久久久| 欧美国产激情| 久久这里只有精品视频首页| 欧美一区二区三区视频| 夜夜嗨av一区二区三区四季av| 欧美日韩美女一区二区| 亚洲免费在线观看视频| 在线视频亚洲| 一区二区三区毛片| 在线一区二区视频| 一区二区日韩免费看| 日韩系列在线| 在线综合亚洲| 国产亚洲精品久久久| 国产精品毛片大码女人| 欧美午夜精品| 国产美女诱惑一区二区| 国产一区二区精品久久| 国产欧美va欧美不卡在线| 国产伦精品一区二区三区视频黑人 | 91久久精品美女| 黄色精品网站| 男人插女人欧美| 免费久久精品视频| 欧美激情亚洲自拍| 久久精品国产久精国产爱| 久久精品色图| 免费高清在线一区| 欧美大片免费观看| 欧美午夜一区二区| 国产午夜精品麻豆| 尤妮丝一区二区裸体视频| 亚洲欧洲一区二区三区久久| 日韩视频不卡| 欧美亚洲专区| 欧美大片在线影院| 99re6热只有精品免费观看 | 久久伊人精品天天| 免费久久99精品国产| 亚洲成色777777在线观看影院| 国产一区二区三区高清| 国产亚洲毛片在线| 日韩视频在线播放| 午夜精品一区二区三区在线视| 欧美日韩中文| 国产精品一区二区在线| 在线国产精品播放| 国产在线视频不卡二| 亚洲第一中文字幕| 亚洲欧美日韩第一区| 毛片一区二区| 亚洲手机视频| 欧美aa在线视频| 国产乱码精品一区二区三| 亚洲国产精品尤物yw在线观看| 国产亚洲一区二区三区在线播放| 欧美精品一区二区三区蜜桃| 欧美日韩国产一级| 极品尤物久久久av免费看| 国产精品99久久久久久有的能看 | 亚洲黄色av一区| 亚洲专区免费| 亚洲第一区色| 久久精品国产视频| 国产精品成人免费视频| 亚洲精品免费观看| 久久影院午夜论| 亚洲一级特黄| 欧美日韩日韩| 亚洲精品视频免费在线观看| 久久精品视频在线播放| 免费不卡欧美自拍视频| 亚洲欧美日韩第一区| 欧美精品免费在线| 亚洲人体1000|