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

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)化的情況,然后我們通過對(duì)2個(gè)重載函數(shù)給于不同的返回值,在編譯時(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ù)的私有或共有都有一個(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 肥仔 閱讀(428) 評(píng)論(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>
            国产日韩欧美在线一区| 欧美mv日韩mv国产网站| 欧美va亚洲va国产综合| 香蕉免费一区二区三区在线观看 | 欧美在线观看视频一区二区| 国内激情久久| 欧美一区二区三区视频免费| 亚洲综合导航| 国产欧美高清| 蜜臀va亚洲va欧美va天堂| 亚洲狼人精品一区二区三区| 亚洲一区www| 亚洲欧美国产制服动漫| 免费在线观看精品| 麻豆久久久9性大片| 久久亚洲捆绑美女| 久久夜色精品国产噜噜av| 你懂的网址国产 欧美| 亚洲精品女av网站| 看欧美日韩国产| 一区二区三区鲁丝不卡| 欧美日韩一区精品| 欧美一区二区福利在线| 亚洲一区免费网站| 亚洲乱码国产乱码精品精可以看| 亚洲一区二区在| 亚洲国产成人精品女人久久久| 欧美性猛交视频| 欧美视频官网| 国产精品久久久久久影院8一贰佰| 亚洲国产精品专区久久| 亚洲国产精品嫩草影院| 久久久99精品免费观看不卡| 国产精品美女主播| 一区二区三区四区国产精品| 欧美一级视频精品观看| 亚洲国产裸拍裸体视频在线观看乱了中文| 一本色道久久综合| 免费亚洲电影| 欧美网站大全在线观看| 国产精品国产精品| 欧美调教vk| 欧美日韩国产美| 欧美体内she精视频| 欧美理论在线播放| 欧美视频福利| 国产日韩欧美三级| 韩国一区二区在线观看| 国内精品一区二区| 欧美精品免费看| 在线观看成人av电影| 亚洲已满18点击进入久久| 久久久久免费视频| 欧美一区二区视频97| 久久精品视频在线看| 午夜在线视频一区二区区别| 亚洲国产三级在线| 欧美高清在线一区二区| 久久久久亚洲综合| 亚洲欧洲av一区二区| 一区二区三区 在线观看视| 欧美va亚洲va日韩∨a综合色| 欧美一二区视频| 亚洲女人小视频在线观看| 日韩香蕉视频| 久久久99精品免费观看不卡| 一区二区三区免费看| 国产精一区二区三区| 亚洲欧美视频一区| 久久久人成影片一区二区三区| 国外成人免费视频| 国产精品久久夜| 欧美午夜精品| 欧美三级电影一区| 欧美日韩国内| 亚洲丝袜av一区| 久久手机精品视频| 国产精品地址| 欧美精品在线观看播放| 亚洲一区国产精品| 亚洲精品一区二区三| 久久免费偷拍视频| 久久激情视频久久| 国产精品99久久久久久有的能看| 亚洲人永久免费| 一区二区精品| 一区二区三区免费在线观看| 亚洲国产美女精品久久久久∴| 在线观看精品| a4yy欧美一区二区三区| 亚洲欧美在线一区| 亚洲欧美激情诱惑| 亚洲综合成人在线| 亚洲午夜激情| 欧美午夜精品| 久久久久久免费| 老司机午夜精品| 亚洲欧美日韩综合aⅴ视频| 99伊人成综合| 欧美性猛片xxxx免费看久爱| 国产精品成人播放| 亚洲国产精品久久久| 欧美中文在线免费| 欧美成熟视频| 久久久国产午夜精品| 欧美日韩在线播放三区| 国产欧美精品日韩精品| 国产精品99久久久久久白浆小说| 宅男噜噜噜66一区二区| 欧美77777| 免费中文日韩| 影音先锋欧美精品| 久久色在线观看| 午夜精品久久久久久久99黑人| 国产精品久久久久av| 91久久精品国产| 久久精品一区二区| 国产精品视频精品视频| 亚洲伦理在线免费看| 亚洲伦理网站| 欧美极品色图| 好吊视频一区二区三区四区 | 一区二区三区精品视频| 欧美尤物一区| 一区二区亚洲精品| 麻豆国产精品777777在线 | 99精品国产在热久久下载| 蜜臀99久久精品久久久久久软件| 久久精品国产999大香线蕉| 国内揄拍国内精品少妇国语| 亚洲激情婷婷| 欧美激情一区在线| 欧美亚洲第一区| 欧美成人精品高清在线播放| 国产精品v片在线观看不卡| 欧美国产欧美综合| 国产精品v欧美精品v日韩| 久久精品在线视频| 久久av一区二区| 韩国av一区二区| 在线亚洲国产精品网站| 亚洲第一天堂av| 亚洲精品美女在线观看| 黄色精品一区二区| 久久久久久久久久码影片| 欧美一区三区三区高中清蜜桃 | 亚洲精品五月天| 韩国一区二区三区美女美女秀| 亚洲第一天堂av| 1024国产精品| 亚洲精品国产精品乱码不99| 亚洲精品视频免费| 好吊日精品视频| 久久尤物电影视频在线观看| 亚洲二区三区四区| 国产欧美日韩视频一区二区| 玖玖国产精品视频| 韩国视频理论视频久久| 久久久久国产精品一区| 午夜精品久久久久99热蜜桃导演| 欧美不卡视频一区发布| 欧美成人高清| 亚洲在线观看视频| 精品福利av| 久久一区二区三区超碰国产精品| 久久精品视频va| 亚洲网站视频| 欧美日韩在线观看视频| 久久久久久69| 亚洲第一在线综合在线| 亚洲婷婷综合色高清在线| 国产一区二区| 欧美午夜精品伦理| 国产一区二区电影在线观看| 久久国产直播| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 一区二区三区高清在线| 国模精品一区二区三区色天香| 亚洲午夜精品久久久久久浪潮| 免费不卡在线观看av| 亚洲欧美中文在线视频| 亚洲国产第一页| 国内综合精品午夜久久资源| 欧美午夜激情视频| 亚洲免费小视频| 午夜精品区一区二区三| 亚洲国产裸拍裸体视频在线观看乱了中文 | 午夜精品一区二区三区电影天堂| 欧美精品一区在线| 噜噜噜在线观看免费视频日韩| 一区二区高清视频在线观看| 最新成人av网站| 亚洲天堂成人在线观看| 在线视频日本亚洲性| 一二三四社区欧美黄| 黄网站免费久久| 国产深夜精品| 狠狠色丁香婷婷综合久久片| 国产精品夜夜夜| 国内精品久久久久久|