https://eel.is/c++draft/basic.life#9
An object o1 is transparently replaceable by an object o2 if
(9.1) the storage that o2 occupies exactly overlays the storage that o1 occupied,
(9.2) o1 and o2 are of the same type (ignoring the top-level cv-qualifiers)
(9.3) o1 is not a const, complete object
(9.4) neither o1 nor o2 is a potentially-overlapping subobject ([intro.object])
(9.5) either o1 and o2 are both complete objects, or o1 and o2 are direct subobjects of objects p1 and p2 , respectively, and p1 is transparently replaceable by p2 .
transparently replaceable object
T is transparently replaceable by U if:
- T and U use the same storage, and
- T and U have the same type (ignoring top-level cv-qualifiers)
T is not transparently replaceable if:
(Use std::launder if need to reuse the address, i.e. const heap objects)
- const objects; const heap objects for using std::launder
- base classes
- [[no_unique_address]] members
When replacing subobjects, (member variables or array elements), the rules apply
recursively to the parent object.
Thus, corner case of std::optional<T>:
If optional<T> holds its T subobject using a [[no_unique_address]] member (in order to pack its bool into the tail padding of T), then you can't use a pointer to the old object to transparently point to the new one.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.