struct X { int n; };
const X *p = new const X{3}; // #1
new (const_cast<X*>(p)) const X{5}; // #2
const int c = std::launder(p)->n;
Assume that the object created at #1
is named obj1
while the object created at #2
is named obj2
. The precondition of std::launder
is that
[ptr.launder] p2 link
p represents the address A of a byte in memory. An object X that is within its lifetime and whose type is similar to T is located at the address A. All bytes of storage that would be reachable through the result are reachable through p (see below).
A byte of storage b is reachable through a pointer value that points to an object Y if there is an object Z, pointer-interconvertible with Y, such that b is within the storage occupied by Z, or the immediately-enclosing array object if Z is an array element.
This rule is a bit obscure. Is the following interpretation a right read?
obj2
will occupy the number of sizeof(X)
bytes beginning with A
. Consider Y
(the object to which std::launder(p)
points) and Z
(namely, obj2
) as the same object, they are pointer-interconvertible, and the sizeof(X)
bytes occupied by obj2
are all within Z
, hence these bytes are all reachable through std::launder(p)
. that is, "All bytes of storage that would be reachable through the result". Whether these bytes are reachable through p
? With the assumption that Y
(namely, the object to which p
points) and Z
are the same object obj1
, which are also the array element of a hypothetical array, as per [basic.compound] p3
an object of type T that is not an array element is considered to belong to an array with one element of type T.
Since these bytes beginning with A
are all within the array of which Z
is an element. Hence, we can say these bytes are all reachable through p
?