我在g 4.6.2(mingw)上运行g -std = c 0x pod_test.cpp.我在A4上收到错误.为什么不是A4 POD?
#include <iostream> #include <new> #include <cstring> using namespace std; struct A { int a,b; char c; }; struct A2 { short buf[1]; }; struct A3:A { }; struct A4:A { short buf[1]; }; static_assert(std::is_pod<A>::value,"Struct must be a POD type"); static_assert(std::is_pod<A2>::value,"Struct must be a POD type"); static_assert(std::is_pod<A3>::value,"Struct must be a POD type"); static_assert(std::is_pod<A4>::value,"Struct must be a POD type"); int main(){}
解决方法
它不是POD,因为它违反了标准布局类的规则:
— either has no non-static data members in the most derived class and
at most one base class with non-static data members,or has no base
classes with non-static data members
继承格中只有一个类可以有非静态数据成员.在这种情况下,两者都是A和A4有.