Вопрос

I ran the below with g++ -std=c++0x pod_test.cpp on g++ 4.6.2 (mingw). I get an error on A4. Why isn't 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(){}
Это было полезно?

Решение

It's not POD because it breaks this rule for standard layout classes:

— 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

Only one class in the inheritance lattice can have non-static data members. In this case, both A and A4 have.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top