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

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>
            国产精品视频第一区| 亚洲色诱最新| 亚洲美女毛片| 91久久久久久| 亚洲剧情一区二区| 亚洲天天影视| 久久久久久久综合| 欧美韩日精品| 99国产麻豆精品| 亚洲天堂视频在线观看| 亚洲午夜精品一区二区| 欧美资源在线观看| 欧美国产日本在线| 国产精品人人做人人爽| 久久亚洲国产精品一区二区| 欧美国产高清| 久久婷婷激情| 亚洲国产综合视频在线观看| 欧美xart系列在线观看| 亚洲日本无吗高清不卡| 亚洲欧美伊人| 欧美精品一区二区三区视频| 国产欧美日韩精品一区| 亚洲精品久久久久久久久久久久 | 欧美高清自拍一区| 夜夜精品视频一区二区| 久久精品毛片| 国产精品国产三级国产aⅴ浪潮| 国产精品一二三视频| 亚洲欧洲一区二区三区久久| 亚洲综合国产| 亚洲国产精品va在线看黑人动漫| 亚洲婷婷国产精品电影人久久| 久久综合一区二区| 国产亚洲精品综合一区91| 一区二区日韩欧美| 欧美激情视频一区二区三区在线播放| 一区二区三区.www| 女同一区二区| **性色生活片久久毛片| 久久精品国产精品亚洲综合| 99精品欧美一区二区三区综合在线 | 黑丝一区二区| 午夜亚洲一区| 在线午夜精品| 欧美日韩一二三四五区| 亚洲毛片在线观看| 亚洲第一精品福利| 久热re这里精品视频在线6| 国产一区二区三区久久久| 先锋影音一区二区三区| 亚洲视频在线一区观看| 国产精品成人一区| 亚洲综合国产| 午夜视频在线观看一区二区三区| 国产精品成人国产乱一区| 中文日韩在线视频| 亚洲精品视频免费| 欧美日韩天天操| 亚洲制服丝袜在线| 亚洲专区一区| 国产日韩欧美二区| 久久精品网址| 久久精品国产99国产精品| 国内精品久久久久国产盗摄免费观看完整版| 亚洲欧美精品一区| 亚洲一区二区免费| 一区二区三区精品| 亚洲女同性videos| 国产一区二区三区高清 | 国内精品免费午夜毛片| 久久久久成人精品| 欧美制服丝袜第一页| 国内精品久久久久伊人av| 久久青青草综合| 免费成人在线视频网站| 亚洲最新色图| 亚洲在线第一页| 精品二区视频| 亚洲精品一区二区网址| 国产精品男gay被猛男狂揉视频| 亚洲伊人伊色伊影伊综合网| 亚洲欧美日韩国产成人精品影院| 国产一区二区三区无遮挡| 欧美国产精品久久| 国产精品久久999| 另类天堂视频在线观看| 欧美极品在线播放| 久久岛国电影| 欧美精品在线观看91| 久久超碰97人人做人人爱| 鲁大师影院一区二区三区| 亚洲午夜小视频| 久久久久久久久久久久久女国产乱 | 久久久久九九九| 99综合电影在线视频| 亚洲免费影视| 亚洲美女少妇无套啪啪呻吟| 亚洲永久精品大片| 亚洲精品日日夜夜| 欧美一区日本一区韩国一区| 亚洲精品国精品久久99热一| 亚洲欧美日韩精品久久奇米色影视| 亚洲成色精品| 午夜欧美不卡精品aaaaa| 亚洲美女在线一区| 久久久久99| 欧美伊久线香蕉线新在线| 欧美日产国产成人免费图片| 久久久青草青青国产亚洲免观| 欧美日本免费| 亚洲国产乱码最新视频| 精久久久久久| 欧美在线视屏| 久久成人久久爱| 国产精品高潮呻吟视频| 亚洲日韩欧美一区二区在线| 伊人久久综合97精品| 午夜精品久久久久久久| 午夜精品视频在线观看| 欧美日韩mp4| 亚洲精品欧美| 99国产麻豆精品| 免费亚洲一区二区| 久久香蕉精品| 国产一区二区三区的电影 | 国内不卡一区二区三区| 亚洲一二三区视频在线观看| 亚洲精品一区二区三区99| 老司机精品视频网站| 久久深夜福利免费观看| 国产日韩欧美二区| 久久国产加勒比精品无码| 欧美在线亚洲在线| 国产视频精品免费播放| 欧美亚洲在线播放| 久久国产主播| 极品裸体白嫩激情啪啪国产精品| 欧美资源在线观看| 免费日本视频一区| 亚洲人久久久| 欧美日韩一区二区三区免费看| 亚洲精品国产欧美| 亚洲一区二区黄| 国产欧美日韩专区发布| 午夜精品久久久99热福利| 久久一区二区三区av| 樱桃国产成人精品视频| 免费欧美在线视频| 亚洲六月丁香色婷婷综合久久| 一区二区三区精品视频| 国产精品美女久久久免费 | 久久国产黑丝| 亚洲国产二区| 亚洲一区二区久久| 国产日韩av高清| 久久在线免费| 日韩午夜黄色| 久久久www| 亚洲精品日韩久久| 国产精品亚洲视频| 久久躁狠狠躁夜夜爽| 日韩一区二区久久| 久久久国产成人精品| 亚洲日韩欧美视频| 国产精品午夜国产小视频| 久久久999精品免费| 亚洲精品免费一二三区| 久久国产夜色精品鲁鲁99| 亚洲国产欧美国产综合一区| 欧美日韩一级大片网址| 久久成人免费网| 亚洲全部视频| 久久中文字幕一区| 亚洲一区日韩| 亚洲激情视频网| 国产亚洲欧洲| 国产精品xxxav免费视频| 久久人人97超碰国产公开结果| av成人老司机| 亚洲国产精品高清久久久| 午夜精品剧场| 一本色道久久综合亚洲精品按摩| 国产欧美精品在线观看| 欧美国产精品中文字幕| 国产午夜精品久久久久久久| 国产自产精品| 你懂的国产精品永久在线| 亚洲精美视频| 亚洲日本中文字幕| 久久av一区二区三区亚洲| 一区二区三区视频观看| 欧美成年网站| 久久综合99re88久久爱| 欧美亚洲第一页| 日韩一级黄色av| 一区二区欧美在线| 欧美精品18| 亚洲人精品午夜在线观看| 激情一区二区三区|