首页 > 其他 > 详细

如何比较一个类型【模板使用】【sizeof用法】

时间:2019-04-07 20:56:00      阅读:161      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;

void testEmptyClass();

struct Empty {

};

struct DummyEmpty {
    char a;

};

template<typename T>
struct EmptyHelper : T { 
    int group[256];
};

struct EmptyHelper2 {
    int group[256];
};

template<typename T>
bool isEmptyClass() {
    return sizeof(EmptyHelper2) == sizeof(EmptyHelper<T>); //if == , then is Empty
}

void testEmptyClass()
{
    cout << "Empty sizeof = " << sizeof(Empty) << endl;
    cout << "DummyEmpty sizeof = " << sizeof(DummyEmpty) << endl;
    cout  << "Empty is " << isEmptyClass<Empty>() << endl;
    cout << "DummyEmpty is " << isEmptyClass<DummyEmpty>() << endl;
}

int main()
{
    testEmptyClass();
}

技术分享图片

 

 

 

 

 

 

如何比较一个类型【模板使用】【sizeof用法】

原文:https://www.cnblogs.com/douzujun/p/10666950.html

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