zoukankan      html  css  js  c++  java
  • C++编译期判断是否能够转型

    #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;
    }
  • 相关阅读:
    不定方程(Exgcd)
    [模板]乘法逆元
    STL-Deque(双端队列)与单调队列的实现
    最优得分 score
    摆书 book
    [模板]树链剖分
    [模板]Splay
    NOIP2013 货车运输
    Java的类类型和类的动态加载
    Java:搜索特定后缀名的文件
  • 原文地址:https://www.cnblogs.com/wwblog/p/3681316.html
Copyright © 2011-2022 走看看