如何比较一个类型【模板使用】【sizeof用法】
#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(); }
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

更多精彩