博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编译期间侦测可转换性
阅读量:7194 次
发布时间:2019-06-29

本文共 1288 字,大约阅读时间需要 4 分钟。

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;
}

 

本文转自莫水千流博客园博客,原文链接:http://www.cnblogs.com/zhoug2020/p/3940374.html,如需转载请自行联系原作者

你可能感兴趣的文章
javascript函数以及作用域简介
查看>>
Windows Phone 编程中页面间传值方法 - [WP开发]
查看>>
apollo实现c#与android消息推送(四)
查看>>
Spring 上下文操作工具类 ContextUtils
查看>>
程序员的智囊库系列之3--分布式文件系统(Distributed file systems)
查看>>
工具推荐|程序员必须知道的11款新型编程工具
查看>>
Python入门之基础语法
查看>>
poj 2714 Random Walk
查看>>
SQL Server中数据的存储
查看>>
jQuery 属性操作方法
查看>>
LeetCode——Longest Consecutive Sequence
查看>>
Activity转换为View和把图片转换为View
查看>>
參考mudo logging写的win下logging
查看>>
云数据库PolarDB(一)
查看>>
[数据结构] 迷宫问题(栈和队列,深搜和广搜)
查看>>
找不到对象?也许你应该这样做
查看>>
Hadoop集群动态服役新的数据节点&&退役数据节点
查看>>
p4137 Rmq Problem / mex
查看>>
python学习之路---day16--面向对象
查看>>
打造一个高逼格的android开源项目——小白全攻略 (转)
查看>>