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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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<voidvoid>    
{
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<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;
}

  張張見識(shí)哦!~~~

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

評(píng)論

# re: C++模版:編譯期檢測(cè)可轉(zhuǎn)換和可繼承 2008-07-07 09:23 cexer

《C++設(shè)計(jì)新思維》里都有詳細(xì)的原理闡述。boost里有很好的實(shí)現(xiàn)。  回復(fù)  更多評(píng)論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1812168
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲激情视频在线播放| 欧美在线观看www| 国产精品久久久久久久午夜片| 久久精品三级| 久久久亚洲综合| 欧美1区3d| 欧美日韩小视频| 国产精品久久久久久av福利软件 | 99热在这里有精品免费| 亚洲毛片视频| 亚洲欧美综合v| 卡一卡二国产精品| 欧美三级视频在线| 国语精品一区| 9i看片成人免费高清| 性视频1819p久久| 久久综合伊人77777尤物| 久热精品视频在线观看一区| 欧美国产日本韩| 欧美成人黄色小视频| 欧美日韩一区在线视频| 国产欧美日韩视频一区二区| 在线看不卡av| 亚洲欧美一级二级三级| 麻豆精品视频| 99精品久久久| 久久久噜噜噜久久人人看| 欧美日韩久久精品| 狠狠综合久久av一区二区老牛| 最新国产拍偷乱拍精品 | 国产日韩精品久久| 1769国内精品视频在线播放| 亚洲色无码播放| 国产区精品视频| 国产精品久久久久久久久久ktv | 国产精品久久一卡二卡| 国产午夜精品全部视频播放| 亚洲黄一区二区| 久久精品国产免费观看| 亚洲精品视频啊美女在线直播| 欧美一区高清| 国产精品国产三级国产专区53| 亚洲国产小视频| 老色鬼精品视频在线观看播放| 艳女tv在线观看国产一区| 欧美大片18| 欧美一级片一区| 国产精品久久久久久福利一牛影视 | 国产亚洲一区二区三区| 亚洲婷婷在线| 亚洲三级免费| 欧美不卡视频| 亚洲电影欧美电影有声小说| 久久久久久久波多野高潮日日| 亚洲天堂成人| 国产精品美女久久久久av超清 | 久久精品国语| 午夜精品久久久久久99热软件| 国产精品成人国产乱一区| 一区二区三区国产精华| 亚洲激情社区| 欧美另类人妖| 一区二区三区四区国产| 99国产精品99久久久久久粉嫩 | 欧美性大战久久久久久久蜜臀| 亚洲伦伦在线| 亚洲精品久久嫩草网站秘色| 欧美日韩不卡合集视频| 9i看片成人免费高清| 日韩视频免费在线观看| 欧美午夜片在线免费观看| 亚洲欧美日韩一区| 亚洲欧美日韩国产中文| 国产性猛交xxxx免费看久久| 久久综合影视| 欧美va亚洲va国产综合| 亚洲天堂免费观看| 午夜亚洲精品| 亚洲福利视频在线| 最新精品在线| 国产精品拍天天在线| 久久久国产一区二区三区| 久久人人97超碰人人澡爱香蕉| 亚洲第一在线视频| 国产乱肥老妇国产一区二| 免费观看一区| 日韩视频中文| 亚洲图片在线观看| 国自产拍偷拍福利精品免费一| 欧美黄色免费| 欧美日韩一级大片网址| 久久久91精品国产| 欧美国产在线电影| 欧美在线网站| 免费日韩成人| 亚洲欧美日韩第一区| 久久久久久自在自线| 国产日韩欧美精品在线| 一区二区三区久久精品| 美国成人直播| 亚洲婷婷国产精品电影人久久| 欧美一区二区网站| 一本色道久久综合亚洲精品不 | 久久精品国产一区二区三区| 欧美激情第9页| 久久国产66| 欧美精品尤物在线| 久久久亚洲欧洲日产国码αv | 狠狠色香婷婷久久亚洲精品| 亚洲乱码一区二区| 在线国产日韩| 亚洲综合色在线| 99这里只有精品| 久久这里有精品15一区二区三区 | 99re成人精品视频| 原创国产精品91| 亚洲欧美久久久| 亚洲视频中文| 欧美精品一区二区三| 欧美阿v一级看视频| 国产在线欧美| 亚洲欧美成人一区二区三区| 一本久久综合亚洲鲁鲁| 欧美91精品| 欧美高清不卡| 在线成人中文字幕| 欧美中文日韩| 久久精品国产亚洲一区二区| 国产精品乱码久久久久久| 日韩午夜在线| 亚洲色图制服丝袜| 欧美午夜片欧美片在线观看| 亚洲三级电影全部在线观看高清| 一区免费观看视频| 久久精品国内一区二区三区| 久久av最新网址| 国产欧美日韩综合| 亚洲一区视频在线| 久久激情综合网| 国产日产欧产精品推荐色 | 欧美亚洲在线视频| 国产精品永久在线| 午夜精品久久久久久99热| 久久爱另类一区二区小说| 国产视频一区欧美| 久久精品国产96久久久香蕉| 久久视频精品在线| 影音先锋一区| 欧美福利视频在线观看| 91久久久国产精品| 在线天堂一区av电影| 欧美性色视频在线| 欧美影院久久久| 欧美18av| 一本久久青青| 国产一区二区三区在线观看免费视频| 午夜精品久久久久| 久久精品国产视频| 亚洲高清精品中出| 国产精品女主播一区二区三区| 亚洲视频一区二区| 久久精品国产一区二区三区| 国模叶桐国产精品一区| 久久美女性网| 亚洲欧美激情一区二区| 中文在线资源观看网站视频免费不卡| 欧美精品久久久久久久免费观看 | 亚洲深夜福利在线| 国产精品日韩久久久| 久久精品视频在线| 亚洲激情偷拍| 久久成人免费网| 亚洲美女色禁图| 国产乱码精品一区二区三区忘忧草| 久久精品九九| 一区二区三欧美| 久久综合久久综合久久| 日韩天堂在线视频| 国产一区二区三区在线观看视频| 欧美大片va欧美在线播放| 亚洲永久免费精品| 亚洲国产免费看| 久久九九全国免费精品观看| 中文国产亚洲喷潮| 在线观看91精品国产麻豆| 国产精品xnxxcom| 欧美激情成人在线| 久久国产夜色精品鲁鲁99| 亚洲视频1区| 亚洲国产另类久久精品| 开心色5月久久精品| 性感少妇一区| 亚洲一品av免费观看| 亚洲日本无吗高清不卡| 黄色在线一区| 国产午夜精品在线| 欧美午夜在线观看| 欧美日韩午夜视频在线观看| 久久综合色8888| 久久久久久亚洲综合影院红桃 |