Posts

Showing posts from December 30, 2018

Standard-layout and tail padding

Image
Clash Royale CLAN TAG #URR8PPP 18 3 David Hollman recently tweeted the following example (which I've slightly reduced): struct FooBeforeBase double d; bool b[4]; ; struct FooBefore : FooBeforeBase float value; ; static_assert(sizeof(FooBefore) > 16); //---------------------------------------------------- struct FooAfterBase protected: double d; public: bool b[4]; ; struct FooAfter : FooAfterBase float value; ; static_assert(sizeof(FooAfter) == 16); You can examine the layout in clang on godbolt and see that the reason the size changed is that in FooBefore , the member value is placed at offset 16 (maintaining a full alignment of 8 from FooBeforeBase ) whereas in FooAfter , the member value is placed at offset 12 (effectively using FooAfterBase 's tail-padding). It is clear to me that FooBeforeBase is standard-layout, but FooAfterBase is not (because its non-static data members do not all have the same access control, [class.prop]/3). But what is it ab