cppreference states that:
Objects with trivial default constructors can be created by using
reinterpret_cast
on any suitably aligned storage, e.g. on memory allocated withstd::malloc
.
This implies that the following is well-defined code:
struct X { int x; };
alignas(X) char buffer[sizeof(X)]; // (A)
reinterpret_cast<X*>(buffer)->x = 42; // (B)
Three questions follow:
- Is that quote correct?
- If yes, at what point does the lifetime of the
X
begin? If on line(B)
, is it the cast itself that is considered acquiring storage? If on line(A)
, what if there were a branch between(A)
and(B)
that would conditionally construct anX
or some other pod,Y
? - Does anything change between C++11 and C++1z in this regard?
Aucun commentaire:
Enregistrer un commentaire