2051. Simplifying alias rules
Section: 6.10 [basic.lval] Status: drafting Submitter: Richard Smith Date: 2014-12-03
The aliasing rules of 6.10 [basic.lval] paragraph 10 were adapted from C with additions for C++. However, a number of the points either do not apply or are subsumed by other points. For example, the provision for aggregate and union types is needed in C for struct assignment, which in C++ is done via constructors and assignment operators in C++, not by accessing the complete object.
Suggested resolution:
Replace 6.10 [basic.lval] paragraph 10 as follows:
If a program attempts to access the stored value of an object through a glvalue whose type is not similar (7.5 [conv.qual]) to one of the following types the behavior is undefined: [Footnote:... —end footnote]
• the dynamic type of the object,
• the signed or unsigned type corresponding to the dynamic type of the object, or
• a char
or unsigned char
type.
Additional note, October, 2015:
It has been suggested that the aliasing rules should be extended to permit an object of an enumeration with a fixed underlying type to alias an object with that underlying type.
вот кстати про юнионы
и вот
Notes
The paragraph defining the strict aliasing rule in the standard contains two additional bullets partially inherited from C:
▪ AliasedType is an
aggregate type or a
union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions).
▪ AliasedType is a (possibly
cv-qualified)
base class of DynamicType.
These bullets describe situations that cannot arise in C++ and therefore are omitted from the discussion above. In C, aggregate copy and assignment access the aggregate object as a whole. But in C++ such actions are always performed through a member function call, which accesses the individual subobjects rather than the entire object (or, in the case of unions, copies the object representation, i.e., via unsigned char). See
core issue 2051.