Related question: Standard-layout and tail padding
Snippet:
#include <iostream>
#include <type_traits>
struct A0
{
int a;
char c;
};
struct B0 : A0
{ char d; };
struct A1
{
int a;
private:
char c;
};
struct B1 : A1
{ char d; };
struct A2
{
private:
int a;
char c;
};
struct B2 : A2
{ char d; };
int main()
{
std::cout << std::is_pod<A0>::value << ' ' << sizeof(B0) << std::endl; // 1 12
std::cout << std::is_pod<A1>::value << ' ' << sizeof(B1) << std::endl; // 0 8
std::cout << std::is_pod<A2>::value << ' ' << sizeof(B2) << std::endl; // 1 8
}
Live demo // Using g++
It's usually said that, when you inherit from a POD type with tail padding, for some historical reasons, the Itanium ABI (that honestly, I don't know what that is) doesn't allow to reause tail padding of a base-class subobject if such subobject is POD.
However, in the third case, A2
is POD because all of its members have the same access control, but B2
is reusing such tail padding. Why is that?
B2::A2
is), unless the class itself is a POD (whichB2
is not). – Justnessis_pod
will be removed in C++20, consider usingis_standard_layout
– Dispersant