zoukankan      html  css  js  c++  java
  • 编译期间侦测可转换性

    template <class T,class U>
    class Conversion
    {
        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)};
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
        using namespace std;

        cout<< Conversion<double,int>::exists << ' ';
        
        getchar();

        return 0;
    }

    sizeof 并不会真有任何表达式被求值。

    ====================================

    template <class T,class U>
    class Conversion
    {
        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 = false};
    };

    class A
    {
        int a;
    };

    class B :public A
    {
        int a;
    };

    #define  SUPERSUBCLASS(T,U)
        (Conversion<const U*,const T*>::exists &&
        !Conversion<const T*,void *>::sameType)

    int _tmain(int argc, _TCHAR* argv[])
    {
        using namespace std;

        cout<< Conversion<double,int>::exists << ' ';

        if( SUPERSUBCLASS(A,B) )
        {
            cout<< "SUPERSUBCLASS<A,B>,A is base class" << ' ';
        }
        
        getchar();

        return 0;
    }

  • 相关阅读:
    表单
    表格的应用
    图像与超链接
    HTML基础(部分标签的应用)
    DataFrame合并数据df.append
    DataFrame处理接口返回数据
    pandas读取excel文件
    数据加载、存储于文件格式:二进制数据格式pickle
    AttributeError: 'DataFrame' object has no attribute 'save'
    web信息收集:获取所有url
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/3940374.html
Copyright © 2011-2022 走看看