#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用法】

 

相关文章:

  • 2022-12-23
  • 2021-08-30
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
猜你喜欢
  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
  • 2021-12-27
  • 2022-01-26
  • 2021-11-14
  • 2021-11-21
相关资源
相似解决方案