c++type-traitsoffsetofobject-layout

Will a standard_layout class's data member have a fixed offset from the object's address?


If a class is_standard_layout, is that sufficient to guarantee that a given non-static data member will always have the same offset from the object's address (i.e. same across different instances of that class, process-wide)?


Solution

  • That effectively has to hold for objects of any type (within a program). Accessing a subobject happens via offsets which are known at compile-time and constant in machine code. For SL types, you can verify this via offsetof - which is guaranteed to work for standard-layout types and basically implies a uniform offset.