Why must members of standard layout classes have the same access control?

274 Views Asked by At

Quoth the standard in [class.prop]/3:

A class S is a standard-layout class if it:

  • has the same access control for all non-static data members,

My understanding (and perhaps I'm mistaken) has always been that C++ access controls do not affect the physical layout of a class in any way -- they're purely a compile-time mechanism to enhance encapsulation. (Or to put it another way, changing a member from private to public is not an ABI-breaking change.)

If this is the case, then what is the reason for this restriction for standard-layout classes?

1

There are 1 best solutions below

4
On BEST ANSWER

Access control very much does affect layout: Within one access contol level, the addresses of non-static data members increase in declaration order, but there is no requirement on addresses of different access levels with respect to one another.

Since standard layout is about addresses of members, the requirement ensures that all member addresses are in a well-defined order.