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 肥仔 阅读(410) 评论(0)  编辑 收藏 引用 所属分类: C++ 模板


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