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 梦在天涯 阅读(1910) 评论(1)  编辑 收藏 引用 所属分类: CPlusPlus

评论

# re: C++模版:编译期检测可转换和可继承 2008-07-07 09:23 cexer

《C++设计新思维》里都有详细的原理阐述。boost里有很好的实现。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

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

搜索

  •  

积分与排名

  • 积分 - 1785065
  • 排名 - 5

最新评论

阅读排行榜