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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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<voidvoid>    
{
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<voidvoid> 
{
    
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<voidvoid> 
{
    
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<doubleint>::exists << ' '
        
<< Conversion<intdouble>::exists2Way<<" "
        
<< Conversion<intdouble>::sameType<<endl;

    cout
<< Conversion<charchar*>::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<voidvoid>::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<voidvoid>::value << std::endl;
    std::cout 
<< "CBase is the super class of CBase : : " << SuperSubclassStrict<CBase, CBase>::value << std::endl;
}

  張張見識哦!~~~

posted on 2007-05-24 18:21 夢在天涯 閱讀(1971) 評論(1)  編輯 收藏 引用 所屬分類: CPlusPlus

評論

# re: C++模版:編譯期檢測可轉換和可繼承 2008-07-07 09:23 cexer

《C++設計新思維》里都有詳細的原理闡述。boost里有很好的實現。  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1817646
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲天堂网在线观看| 欧美精品在线观看一区二区| 久久男人资源视频| 精品不卡一区| 欧美 日韩 国产在线| 亚洲国产一区二区三区a毛片| 亚洲国产mv| 欧美精品国产一区| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 亚洲日本久久| 亚洲网站在线| 国产精品久久77777| 欧美一区视频| 亚洲国产裸拍裸体视频在线观看乱了中文 | 国产综合香蕉五月婷在线| 久久精品亚洲乱码伦伦中文| 欧美成人精品三级在线观看 | 亚洲午夜影视影院在线观看| 国产精品综合| 麻豆国产精品777777在线| 一本色道久久加勒比88综合| 久久久久久欧美| 一区二区欧美日韩视频| 国产伦精品一区二区三区在线观看| 久久一区二区三区av| 亚洲视频一区在线| 免费亚洲一区二区| 先锋影音网一区二区| 亚洲人成免费| 国产一区二区三区在线观看免费视频| 欧美激情视频一区二区三区免费 | 中文有码久久| 好吊视频一区二区三区四区| 欧美成人资源| 香蕉成人久久| 一区二区三区色| 亚洲福利电影| 久久免费精品日本久久中文字幕| 亚洲视频一区二区在线观看| 韩日欧美一区二区| 国产精品久久久久久av下载红粉| 久久午夜影视| 久久国产精品久久久久久| 日韩手机在线导航| 亚洲电影成人| 久久久久久日产精品| 亚洲欧美视频在线| 亚洲深夜福利网站| 日韩午夜激情| 亚洲高清视频在线| 国语对白精品一区二区| 国产精品网红福利| 欧美午夜视频一区二区| 欧美片在线播放| 另类天堂视频在线观看| 欧美自拍偷拍| 欧美一区二区三区在| 亚洲一区二区视频在线| 艳女tv在线观看国产一区| 亚洲精品国产精品国自产观看浪潮 | 欧美激情一区二区| 久久精品动漫| 久久电影一区| 欧美在线视频免费| 午夜精品久久久久久99热| 亚洲一区二区在线看| 中文一区二区| 亚洲直播在线一区| 亚洲欧美制服中文字幕| 亚洲无亚洲人成网站77777 | 欧美激情在线播放| 欧美成人一区二区三区在线观看| 每日更新成人在线视频| 蜜臀久久99精品久久久久久9 | 久久综合九色| 裸体歌舞表演一区二区| 欧美一级一区| 亚洲欧美日韩在线| 亚洲一线二线三线久久久| 亚洲综合清纯丝袜自拍| 在线一区二区视频| 99精品国产在热久久| 一区二区成人精品| 亚洲一区精品电影| 久久www免费人成看片高清| 欧美一区亚洲| 久久综合给合久久狠狠狠97色69| 女生裸体视频一区二区三区| 欧美ed2k| 日韩一区二区精品| 性高湖久久久久久久久| 久久天天躁狠狠躁夜夜av| 牛牛国产精品| 欧美性淫爽ww久久久久无| 国产日本欧美一区二区三区在线| 狠狠久久亚洲欧美专区| 在线成人亚洲| 99国内精品久久久久久久软件| 亚洲午夜精品一区二区三区他趣| 午夜精品久久久久久久蜜桃app| 欧美在线三级| 欧美激情偷拍| 亚洲一级黄色片| 久久免费高清视频| 欧美日韩国产不卡| 国产午夜精品久久久久久久| 在线观看91精品国产入口| 日韩亚洲成人av在线| 久久成人在线| 亚洲人成在线观看一区二区| 亚洲一区二区在线看| 久久精品72免费观看| 欧美福利一区二区三区| 国产欧美精品一区二区色综合| 亚洲国产一区二区三区青草影视| 亚洲视频axxx| 欧美不卡高清| 一区二区三区国产盗摄| 久久久蜜桃精品| 欧美调教视频| 亚洲精品日产精品乱码不卡| 欧美亚洲在线视频| 亚洲区欧美区| 久久精品视频免费| 国产精品久久久久久久浪潮网站| 亚洲高清资源| 久久久精品国产99久久精品芒果| 亚洲国产精品久久| 久久精品视频一| 国产精品日日摸夜夜摸av| 亚洲欧洲一区二区三区在线观看| 久久高清一区| 亚洲素人一区二区| 欧美国产日本高清在线| 国产午夜一区二区三区| 亚洲综合另类| 亚洲伦理在线| 久久综合福利| 国产综合欧美| 久久国产视频网| 亚洲视频综合在线| 欧美日韩一区二区在线| 亚洲激情成人网| 欧美/亚洲一区| 久久国产精品第一页| 国产欧美一区二区三区久久| 亚洲开发第一视频在线播放| 麻豆国产精品777777在线 | 久久蜜桃资源一区二区老牛| 国产欧美在线观看| 午夜免费在线观看精品视频| 99精品欧美一区二区三区| 欧美不卡视频一区发布| 在线电影国产精品| 美国成人直播| 久久噜噜亚洲综合| 极品日韩久久| 免费日韩av片| 开心色5月久久精品| 一区二区三区在线观看视频| 久久精品视频免费观看| 亚洲欧美日韩综合aⅴ视频| 国产精品老牛| 久久爱www| 久久er99精品| 精品不卡一区| 欧美激情第4页| 欧美福利视频网站| 一区二区三区欧美亚洲| 亚洲精品一区在线| 国产精品高潮呻吟| 欧美一区二区三区四区在线观看地址| av成人手机在线| 国产精品久久影院| 欧美在线视频观看| 久久久久久有精品国产| 亚洲国产精品成人一区二区 | 一区二区三区日韩精品| 国产精品国产三级国产专播品爱网| 亚洲综合国产精品| 午夜欧美大片免费观看| 国语自产精品视频在线看| 久久久国产一区二区| 鲁大师影院一区二区三区| 亚洲精品视频在线观看网站| 99精品国产99久久久久久福利| 欧美无乱码久久久免费午夜一区| 欧美一级久久| 另类亚洲自拍| 一区二区三区波多野结衣在线观看| 一区二区毛片| 国产伊人精品| 亚洲激情在线视频| 国产精品成人在线观看| 久久漫画官网| 欧美韩日一区二区| 欧美一级夜夜爽| 欧美福利网址| 久久精品亚洲一区二区| 欧美99久久|