Relaxing POD Requirements in C 11: Exploring Its Rationale
The C 11 standard introduced a revised definition for plain old data (POD) types, which has raised some questions regarding its design choices.
Restrictions on Data Member Access Control
Initially, PODs were required to have members with the same access control, preventing different access levels for data members. If this restriction was lifted, it could lead to inconsistent behavior when attempting to access members from objects of varying permissions levels.
Prohibition of Overlapping Data Members
Another surprise in the definition is the prohibition of base classes with the same type as the first data member. This restriction stems from the need for the first member and the complete object to have the same address. If they overlapped, casting an object address to the type of its first member could lead to ambiguity.
Limitation on Multiple Data Member Classes
The requirement that only at most one base class have data members restricts the potential for ambiguities. If multiple base classes contained data members, the standard would need to specify an allocation order for these members across classes, which would impact the flexibility of memory layout implementations.
The Rationale for These Restrictions
These restrictions ensure that standard-layout classes meet specific criteria, allowing certain optimizations:
Conclusion
The revised POD definition in C 11 provides a more flexible framework for standard-layout classes while safeguarding critical considerations related to object memory layout and data member access. These restrictions ensure the consistency and reliability of POD behavior, despite their potential for unusual constructions like the example provided.
The above is the detailed content of Why Did C 11 Restrict POD Type Definitions?. For more information, please follow other related articles on the PHP Chinese website!