There are many many questions from surprised C++ programmers who notice that tail padding is reused only when the base class is not a POD type.
For example, the following code works on x86-64 gcc (godbolt link):
struct A {
int a;
bool aa;
};
struct B : A {
bool b;
};
struct C : B {
bool c;
};
static_assert(sizeof(A) == 8);
static_assert(sizeof(B) == 12);
static_assert(sizeof(C) == 12);
This means that the field c
is placed into the padding of B
, but the field b
is not placed into the padding of A
.
The linked questions only explain that a particular ABI specifies this layout, and one answer goes as far as to say that reusing the tail padding of a POD type "breaks common assumptions a programmer would make", and so "basically any sane compiler won't do tail padding reuse" for such types. However, this explanation is unsatisfactory because it doesn't actually guarantee anything.
Does the standard guarantee that the tail padding of a POD type will not be reused by a derived class?