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

C++ Programmer's Cookbook

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

C++模版:編譯期檢測可轉換和可繼承

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

//C++模版:編譯期檢測可轉換和可繼承
//一般我們使用dynamic_cast<>在運行期進行轉化,但是對于模版編程,我們可以實現(xiàn)編譯時類型檢測。利用了模版技術,sizeof的編譯期就可以得到結果和函數(shù)的重載技術。

//比如我們檢測T是否可以轉化為U,我們實現(xiàn)一個函數(shù)Test和他的一個重載,函數(shù)的參數(shù)分別是U和。。。
//參數(shù)U和。。。分別對應可以轉化為U和不可以轉化的情況,然后我們通過對2個重載函數(shù)給于不同的返回值,在編譯時用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的構造函數(shù)的私有或共有都有一個零時的T供sizeof使用。

//T是U的基類或T和U是同一類的2個別名。
//注意SuperSubClass傳入的模版參數(shù)與內部調用Conersion函數(shù)的參數(shù)傳入順序,在參數(shù)前面加了const volatile限定了不能使用重載的類型轉化函數(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;
}

  張張見識哦!~~~

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

評論

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

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

公告

EMail:itech001#126.com

導航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1812196
  • 排名 - 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>
              99亚洲视频| 蜜桃av噜噜一区二区三区| 欧美连裤袜在线视频| 一区二区免费在线视频| 久久中文字幕一区| 亚洲夜间福利| 99国产精品久久久久老师 | 亚洲国产美女| 欧美国产日韩一区二区| 亚洲一区在线观看视频 | 国产精品人人做人人爽| 国产精品第一页第二页第三页| 亚洲最黄网站| 日韩亚洲欧美中文三级| 国产精品美女黄网| 午夜精品久久久久久久99水蜜桃| 这里是久久伊人| 亚洲国产精品99久久久久久久久| 中文亚洲字幕| 亚洲在线观看免费视频| 一区二区在线视频| 国产精品亚洲综合色区韩国| 国产日韩欧美a| 国产精品va在线| 久久综合亚洲社区| 亚洲一区三区在线观看| 亚洲国产综合在线看不卡| 国产精品日本一区二区| 久久一区二区三区国产精品| 99热在这里有精品免费| 欧美~级网站不卡| 亚洲一区二区三区四区视频| 亚洲欧洲av一区二区三区久久| 亚洲日韩成人| 亚洲国产精品久久人人爱蜜臀| 国内外成人在线| 国产欧美日韩免费| 欧美婷婷久久| 欧美日韩亚洲一区| 国产一区二区三区四区五区美女| 国产精品国内视频| 欧美视频国产精品| 欧美日韩不卡在线| 久久先锋影音| 久久久午夜视频| 亚洲毛片一区二区| 欧美在线一区二区| 欧美一级大片在线观看| 亚洲欧美日韩一区| 亚洲国产精品久久精品怡红院| 欧美国产专区| 午夜一区二区三区在线观看| 亚洲欧美在线x视频| 亚洲欧美视频一区| 欧美无砖砖区免费| 国产精品一区一区三区| 亚洲日本aⅴ片在线观看香蕉| 亚洲激情二区| 欧美成人免费在线视频| aⅴ色国产欧美| 蜜桃精品一区二区三区| 亚洲成人在线视频网站| 欧美成人综合网站| 欧美先锋影音| 夜夜嗨av色综合久久久综合网| 艳女tv在线观看国产一区| 女同性一区二区三区人了人一 | 欧美激情一区二区三区全黄 | 狠狠网亚洲精品| 永久555www成人免费| 亚洲久久一区| 亚洲欧美在线免费观看| 一区二区三区日韩欧美| 小处雏高清一区二区三区| 老司机精品视频一区二区三区| 在线性视频日韩欧美| 在线一区二区日韩| 亚洲人成网站999久久久综合| 欧美一级在线播放| 欧美精品在欧美一区二区少妇| 欧美成人午夜激情在线| 日韩视频欧美视频| 久久精品亚洲乱码伦伦中文| 亚洲盗摄视频| 欧美午夜精品久久久久久孕妇| 欧美极品aⅴ影院| 国产精品视频不卡| 欧美亚洲一区二区在线观看| 亚洲欧洲一区二区天堂久久| 久久成人av少妇免费| 性欧美xxxx大乳国产app| 国产日韩在线视频| 日韩亚洲欧美成人| 老鸭窝亚洲一区二区三区| 欧美va亚洲va国产综合| 国产噜噜噜噜噜久久久久久久久| 国产在线精品成人一区二区三区| 国产精品美女午夜av| 久久久久国产精品www| 日韩一级在线| 欧美一区三区二区在线观看| 国产一区二区三区直播精品电影 | 久久久久国产免费免费| 日韩亚洲在线观看| 国产综合色产| 午夜精品久久久久久久白皮肤 | 欧美国产日本| 免费成人高清视频| 久久精品国产精品亚洲| 国产精品久久久久久久浪潮网站| 亚洲久久成人| 亚洲黄网站在线观看| 久久久久亚洲综合| 国内外成人免费视频| 性欧美8khd高清极品| 欧美高清视频在线播放| 久久免费观看视频| 激情丁香综合| 久久人91精品久久久久久不卡 | 影音欧美亚洲| 久久激情视频免费观看| 巨乳诱惑日韩免费av| 亚洲日本欧美日韩高观看| 欧美/亚洲一区| 国产欧美视频一区二区三区| 亚洲伦理在线| 亚洲啪啪91| 久热精品在线| 奶水喷射视频一区| 国产偷国产偷精品高清尤物| a91a精品视频在线观看| 日韩视频一区| 欧美精品亚洲精品| 亚洲第一免费播放区| 狠狠色综合一区二区| 欧美一级夜夜爽| 久久精品人人爽| 国产日韩精品在线播放| 在线一区二区三区四区五区| 亚洲欧美国产77777| 欧美日韩国产一区二区三区地区| 亚洲激情成人| 99国产精品国产精品毛片| 欧美大片在线看| 亚洲激情av| 亚洲天堂成人在线观看| 欧美日韩国产精品自在自线| 亚洲狼人精品一区二区三区| 一区二区三区日韩精品| 欧美全黄视频| 99riav1国产精品视频| 99热这里只有精品8| 欧美日韩精品一区| 亚洲无玛一区| 久久噜噜噜精品国产亚洲综合| 狠狠狠色丁香婷婷综合久久五月| 欧美在线free| 欧美h视频在线| 一区二区三区精品国产| 国产精品ⅴa在线观看h| 亚洲伊人久久综合| 久久一日本道色综合久久| 亚洲国产一区二区三区青草影视| 欧美肥婆bbw| 在线视频亚洲| 美日韩在线观看| 一本到高清视频免费精品| 国产精品拍天天在线| 久久av一区二区三区| 亚洲第一精品久久忘忧草社区| 亚洲小视频在线| 国产一区二区高清| 亚洲国产精选| 午夜精品福利在线| 黄色亚洲在线| 欧美理论电影在线播放| 午夜精品国产| 亚洲日本久久| 久久精品九九| 亚洲日产国产精品| 国产亚洲欧美中文| 欧美日本在线视频| 欧美在线视频观看| 一本久道久久综合婷婷鲸鱼| 久久男人资源视频| 亚洲专区在线视频| 亚洲人成啪啪网站| 国产视频亚洲| 欧美香蕉视频| 欧美成人在线免费观看| 新片速递亚洲合集欧美合集| 亚洲精品美女在线观看| 看片网站欧美日韩| 欧美一区二区在线看| 99在线精品免费视频九九视| 好吊成人免视频| 国产精品另类一区| 欧美福利视频在线观看| 久久另类ts人妖一区二区 | 亚洲欧美日韩久久精品|