Jan 31, 2025

[C++] transparently replaceable

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 .


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.