This is a follow-up to this other question
I was trying to establish at compile time whether a specific implementation had added unnamed padding inside a struct. Spe
There may in theory be padding at the end of the struct after t
, which your assert does not catch (which may or may not be intended). Your assumption is otherwise correct and this is perfectly fine use of offsetof
and static_assert
, to detect padding anywhere between member variables.
A better alternative might be:
static_assert( offsetof(struct quad, t) == sizeof(struct quad)-sizeof(int),
This also catches padding at the end of the struct. In addition, it makes the assertion more flexible in case the struct members are changed during code maintenance.