10

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(){}
Jonas
  • 121,568
  • 97
  • 310
  • 388

1 Answers1

16

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • there is at most one base class with non-static data members. In all examples. How does this rule apply? – Mordachai Mar 06 '12 at 14:46
  • 1
    "has no non-static data members in the most derived class **and** at most one base class with non-static data members". `A4` has non-static data members. – R. Martinho Fernandes Mar 06 '12 at 14:47
  • I think i linked that to you. I remember its sect 7 -lookups- ok 9.0.7. Accept when i can. I overlooked the ',' before the or. No wonder why 'either' made no sense. –  Mar 06 '12 at 14:47
  • 1
    @Mordachai: "no non-static data members in most derived class *and* at most one base class with non-static data members". – Cat Plus Plus Mar 06 '12 at 14:47
  • Hmm, I also didn't realize that you essentially can't use inheritence. The committee dropped the ball on standard layout types then -- it's of no more use than the old POD definition. Here's to another 10 years coding standards non-compliant structures. :( – edA-qa mort-ora-y Mar 06 '12 at 15:45
  • @edA-qamort-ora-y Yes, you can. See my answer here: http://stackoverflow.com/a/7189821/46642 – R. Martinho Fernandes Mar 06 '12 at 15:49
  • I meant _effective inheritence_, you know, where each item in the chain has data. A lot of C-style PODs emulate inheritance by having a struct as the first member. It would have been nice to be able to model the same data with proper inheritence -- and it would have been simple to specificy this should work. But now, even if you wish to communicate with another C++ program, you can't use inheritence. – edA-qa mort-ora-y Mar 06 '12 at 16:05
  • @edA-qamort-ora-y Oh, My comment was a reply to your now deleted comment about trivial types. Standard layout types can't, but trivial types can. Obviously if what you need is standard layout, then yeah, you're screwed. – R. Martinho Fernandes Mar 06 '12 at 16:12
  • @edA-qamort-ora-y: The standard layout changes were basically codifying existing practice. What you're suggesting would be a fundamental change, forcing the standards committee to actually make implementations put base class members in a certain place in terms of layout. That's not a good idea; it would break the ABI for any compilers that did it differently from the newly standardized way. It's a non-starter. – Nicol Bolas Mar 06 '12 at 16:12
  • @edA if you know to do it better than the committee you can post your ideas on comp.std.c++. – Johannes Schaub - litb Mar 08 '12 at 07:29