首页 > 编程语言 > 详细

C++编译期判断是否能够转型

时间:2014-04-23 21:24:08      阅读:665      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include <iostream>
#include <vector>
using namespace std;

template<class T,class U>
class Conversion
{
private:
    typedef char Small;
    class Big{char dummy[2];};
    static Small test(U);
    static Big test(...);
    static T makeT();
public:
    enum{ exists =
        sizeof(test(makeT()))==sizeof(Small) };
    enum{ exists2Way =
        exists && Conversion<U,T>::exists};
    enum{
        sameType=false};
};

template<class T>
class Conversion<T,T>
{
public:
    enum{exists=1,exists2Way=1,sameType=1};
};

#define SUPERSUBCLASS(T,U)\
    (Conversion<const U*,const T*>::exists &&     !Conversion<const T*,const void*>::sameType)
#define SUPERSUBCLASS_STRICT(T,U)\
    (SUPERSUBCLASS(T,U) &&     !Conversion<const T,const U>::sameType)
class Shape
{
public:
    virtual void display()
    {
        cout<<"Shape"<<endl;
    }
};

class Circle:public Shape
{
public:
    virtual void display()
    {
        cout<<"Circle()"<<endl;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    cout<<Conversion<double,int>::exists<<endl;
    cout<<SUPERSUBCLASS(Shape,Circle)<<endl;
    cout<<SUPERSUBCLASS_STRICT(Shape,Circle)<<endl;
    return 0;
}
bubuko.com,布布扣

 

C++编译期判断是否能够转型,布布扣,bubuko.com

C++编译期判断是否能够转型

原文:http://www.cnblogs.com/wwblog/p/3681316.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!