Executing the following program
#include <cassert> #include <cstddef> struct S { char x; int y; } __attribute__((packed, aligned(8))); struct alignas(8) T { char x; int y; } __attribute__((packed)); int main() { assert(offsetof(S, x) == 0); assert(offsetof(S, y) == 1); assert(offsetof(T, x) == 0); assert(offsetof(T, y) == 1); }
fails with assertion
a.out: a.cc:19: int main(): Assertion `offsetof(T, y) == 1' failed.
The layout if T is incorrect, because it's computed and effectively cached when checking for alignas under-aligning the structure, however, this happens before __attribute__((packed)) is processed.
This patch moves the processing of attributes before the alignas under-alignment check.
More generally: "before checking the layout".