Yes, the standard guarantees that it is safe to assert that. The relevant term here is layout compatible.
The standard defines that term in two parts. First it defines what a common initial sequence of data members is (but only for standard-layout structs): It is the sequence of data members that are equivalent between the two structs. The standard includes an example, but I'll use a slightly different one to avoid some technicalities:
struct A { int a; char b; };
struct B { int b1; char b2; };
struct C { int x; int y; };
In that example, the common initial layout of A
and B
is both of their members, while for A
and C
it is only their first respective members. It then defines structs as layout compatible if the common initial layout is simply the whole class.
If you have instances of two different layout-compatible types, like A
and B
in the above example, you *can* assume they have the same size:
static_assert(sizeof(A) == sizeof(B));
However, you *cannot* (in theory) cast between them without invoking undefined behaviour, because that violates aliasing rules:
A a{1, 'a'};
B* b = reinterpret_cast<B*>(&a); // undefined behaviour!
do_something_with(b);
What you can do, subject to the usual const
/volatile
rules as well as rules about data members being trivial (see When is a type in c++11 allowed to be memcpyed?), is use memcpy
to get data between layout-compatible structs. Of course, that wouldn't be possible of padding between members could be randomly different, as the current top answer suggests.
A a{1, 'a'};
B b;
memcpy(&b, &a, sizeof(b)); // copy from a to b
do_something_with(b);
If do_something_with
takes its argument by reference and modifies it then you would then need to copy back from b
to a
to reflect the effect there. In practice this would usually be optimised to be what you would expect the above cast to do.
The answer by atomsymbol gives an example that appears to contradict everything above. But you asked about what was in the standard, and a #pragma
that affects padding is outside of what is covered by the standard.