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

woaidongmao

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

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

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

//C++模版:編譯期檢測可轉(zhuǎn)換和可繼承
//一般我們使用dynamic_cast<>在運行期進行轉(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<void, void>   
{
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<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 肥仔 閱讀(434) 評論(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>
            欧美在线一级va免费观看| 欧美专区在线观看| 中文亚洲字幕| 一区二区三区 在线观看视| 一本到高清视频免费精品| 亚洲精品资源美女情侣酒店| 亚洲人被黑人高潮完整版| 欧美www视频在线观看| 欧美电影免费观看高清完整版| 免费av成人在线| 欧美电影资源| 亚洲精品激情| 亚洲午夜日本在线观看| 亚洲欧美在线另类| 欧美综合国产精品久久丁香| 久久九九热免费视频| 老牛国产精品一区的观看方式| 欧美高清在线一区| 欧美视频精品一区| 国产日韩欧美亚洲一区| 激情久久综艺| 日韩视频不卡| 午夜精品美女自拍福到在线 | 亚洲欧美一区二区三区久久| 欧美一区二区三区在线看| 久久久人成影片一区二区三区观看| 久久婷婷久久一区二区三区| 欧美日本中文| 国产精一区二区三区| 在线精品福利| 亚洲少妇诱惑| 久久久久久久网站| 亚洲国产精品毛片| 亚洲影视综合| 久久视频一区二区| 欧美涩涩网站| 精品成人一区二区| 一本色道久久精品| 久久精品国产综合| 亚洲精品美女在线观看| 午夜欧美精品| 欧美精品一区二区三区很污很色的| 国产精品一区二区久久| 亚洲国产欧美一区二区三区丁香婷| 亚洲图片激情小说| 麻豆精品一区二区综合av | 美女在线一区二区| 国产精品高清网站| 在线播放日韩专区| 亚洲性夜色噜噜噜7777| 美日韩精品免费| 妖精成人www高清在线观看| 久久国产加勒比精品无码| 欧美激情综合在线| 国内精品99| 亚洲一区二区免费看| 免费观看欧美在线视频的网站| 一本色道久久综合精品竹菊| 久久久久久夜| 国产精品日本精品| 日韩视频在线你懂得| 久久久久久精| 亚洲午夜一区二区| 欧美经典一区二区| 亚洲国产成人av| 久久久久久久999| 在线视频日本亚洲性| 欧美激情第二页| 红桃av永久久久| 香蕉视频成人在线观看| 亚洲日本中文字幕| 久久综合亚洲社区| 黄色成人av| 欧美一区二区三区免费观看| 亚洲精品一区二区三区不| 久久免费的精品国产v∧| 国产美女扒开尿口久久久| 一本一本久久| 亚洲国产精品尤物yw在线观看| 久久久久国产精品一区| 国产亚洲综合性久久久影院| 午夜精品www| 日韩一区二区免费高清| 欧美另类极品videosbest最新版本 | 男女精品视频| 欧美诱惑福利视频| 国产精品揄拍500视频| 亚洲一区二区在线免费观看视频| 亚洲黄色免费电影| 免费高清在线一区| 亚洲黄色三级| 欧美大片在线看| 另类春色校园亚洲| 亚洲高清不卡在线观看| 噜噜爱69成人精品| 久久人人97超碰国产公开结果| 国产尤物精品| 久久伊人亚洲| 久久久综合精品| 亚洲丶国产丶欧美一区二区三区| 久久综合久色欧美综合狠狠| 久久久精品日韩欧美| 在线成人激情黄色| 欧美成人xxx| 欧美va亚洲va香蕉在线| 亚洲区国产区| 亚洲精品激情| 国产精品www994| 香蕉视频成人在线观看| 午夜精品视频一区| 狠狠干综合网| 欧美第十八页| 欧美久久久久久久| 亚洲一区二区三区久久| 亚洲五月六月| 国产一区二区久久| 欧美成年人视频| 欧美激情一区二区三区高清视频| 一区二区三区四区五区精品| 正在播放亚洲一区| 国产亚洲精品aa午夜观看| 蜜桃精品久久久久久久免费影院| 久久永久免费| 亚洲色图自拍| 欧美综合激情网| 亚洲精品视频在线看| 一本色道久久加勒比88综合| 国产精品网站在线| 欧美成年人视频网站欧美| 欧美国产先锋| 欧美一区二区三区在| 久久一综合视频| 国产精品99久久99久久久二8| 亚洲欧美国产高清| 一区在线播放| 亚洲日韩视频| 国产日韩欧美日韩大片| 欧美激情女人20p| 国产精品久久久久久影视| 久久久久综合网| 欧美精品在线一区二区| 性欧美激情精品| 欧美 日韩 国产 一区| 亚洲综合精品四区| 久久欧美肥婆一二区| 一区二区三区精品| 久久精品99无色码中文字幕| 亚洲美女精品久久| 午夜欧美不卡精品aaaaa| 日韩写真在线| 久久高清免费观看| 亚洲一区二区三区三| 久久久91精品国产| 亚洲一区在线直播| 久热精品在线| 欧美一区二区三区久久精品| 男人的天堂亚洲| 久久久视频精品| 国产精品久在线观看| 欧美福利视频在线| 国产一区成人| 99精品国产热久久91蜜凸| 亚洲高清不卡| 欧美中文字幕第一页| 亚洲一区欧美二区| 免费在线观看精品| 久久综合精品一区| 国产精品家庭影院| 亚洲国产精品久久久久秋霞影院 | 一本久道久久综合婷婷鲸鱼| 伊人春色精品| 午夜亚洲性色福利视频| 一本色道久久99精品综合| 久久午夜视频| 久久精彩视频| 国产精品人成在线观看免费 | 久久久午夜视频| 国产精品高清免费在线观看| 亚洲精品1234| 亚洲国产另类久久久精品极度| 性刺激综合网| 午夜免费在线观看精品视频| 欧美人与禽性xxxxx杂性| 欧美aaaaaaaa牛牛影院| 国产一区久久| 午夜在线观看免费一区| 欧美一二区视频| 国产精品久久久久久久久搜平片| 最新国产の精品合集bt伙计| 亚洲高清在线观看| 久久久久久久999精品视频| 久久国产免费| 国产欧美日韩不卡| 亚洲影视九九影院在线观看| 在线综合亚洲欧美在线视频| 欧美久久婷婷综合色| 亚洲高清在线观看一区| 亚洲精品国偷自产在线99热| 免费观看在线综合| 欧美激情在线免费观看|