For a standard layout class, it is legal to convert a pointer to an object of the class to a pointer to the first non-static data member of the class. Now assume that the first non-static data member happens to be a private member. Is this a legal way to modify a private data member outside class' member function?
If not, could you indicate what part of standard prevents it?
Note: The purpose here is to understand the standard better, not to do this in actual code.
"assume that the first non-static data member happens to be a private member"
That's fine, if all non-static
data members are private
. From the C++20 final working draft:
[class.union]
), or[class.mem]
), orc
such that a
and c
are pointer-interconvertible, and c
and b
are pointer-interconvertible.11.2 Properties of classes [class.prop]
S
is a standard-layout class if it: