In C++, when do you need std::launder or C++23's std::start_lifetime_as instead of a plain reinterpret_cast to use an object in raw storage?
answer
- a pointer is not just an address
- new returns the pointer to keep
- launder recomputes a pointer to a live object
- start_lifetime_as creates one from bytes
basics
~20 sA reinterpret_cast pointer into storage need not point to the object there. Keep the pointer placement new returns, use std::launder to recompute a pointer to an existing object, and C++23's std::start_lifetime_as to turn existing bytes into an object.
solid answer
~50 sIn C++ a pointer value points to a specific object, not merely to an address. After `new (storage) Entry{...}`, the pointer **returned** by placement new points to the `Entry`; `reinterpret_cast<Entry*>(storage)` still points to the byte array. `std::launder` (C++17) takes such a pointer and returns one to the object that is actually within its lifetime at that address, so the right uses are recomputing a pointer from storage and reaching a replacement object the old pointer may not designate: since C++20 that is only when the old complete object was const or the object is a base subobject. `std::start_lifetime_as<T>(p)` (C++23) solves another problem: storage holds bytes, such as a file header read into a buffer, and no `T` object exists. It implicitly creates a `T` of an implicit-lifetime type whose value comes from those bytes. libc++ in Apple clang 21 lacks it; libstdc++ in GCC 16.2 has it. The portable fallback is `memcpy` into a local.
code
cpp · 19 lines#include <cstddef>
#include <cstdio>
#include <new>
struct Entry {
const int id; // const member
double weight;
};
int main() {
alignas(Entry) std::byte storage[sizeof(Entry)];
Entry* first = new (storage) Entry{1, 0.5}; // keep the pointer new returns
std::printf("first id %d\n", first->id);
first->~Entry();
Entry* second = new (storage) Entry{2, 0.25};
std::printf("via second %d\n", second->id);
std::printf("via old pointer %d\n", first->id); // OK since C++20: transparently replaceable
Entry* recomputed = std::launder(reinterpret_cast<Entry*>(storage));
std::printf("via launder %d\n", recomputed->id);
second->~Entry();
}go deeper
Know that placement new returns the pointer to use for the object it built, and that casting raw bytes to a struct pointer is risky.
Explain that a pointer designates an object, not just an address, and when old pointers keep working after an object is replaced in the same storage.
Use std::launder correctly in storage-based wrappers, know implicit-lifetime types and C++20 implicit object creation, and choose start_lifetime_as or memcpy for bytes read from files.
Decide whether a codebase may rely on C++23 lifetime functions given library support, and keep raw-storage code confined to a few reviewed types.
## Objects, not addresses C++ defines a pointer value as pointing to an **object** (or past the end of one), not to a bare address. Two pointers can hold the same address and still point to different objects: the first element of a `std::byte` array and an `Entry` constructed in that array, for example. Compilers rely on this. They may assume a pointer to one object does not reach a different object created later in the same storage, and on that basis keep values in registers. The tools in this answer exist to give you a pointer that really points to the object you mean. ## Placement new: keep the pointer it returns ```cpp alignas(Entry) std::byte storage[sizeof(Entry)]; Entry* e = new (storage) Entry{1, 0.5}; // e points to the Entry ``` The pointer returned by the `new`-expression points to the new object. `reinterpret_cast<Entry*>(storage)` yields a value with the right address that, by the standard's rules, still points to the byte array, because the array and the `Entry` are not pointer-interconvertible. Keeping `e` is the simplest correct code and needs no further tool. ## When std::launder is needed `template<class T> constexpr T* launder(T* p) noexcept;` (C++17, from P0137R1) has a precondition: an object of a type similar to `T` is within its lifetime at the address `p` represents. It returns a pointer to **that** object. Use it when: 1. **You must recompute the pointer from storage**, as in a container like `std::optional` that stores only the buffer: `std::launder(reinterpret_cast<Entry*>(storage))`. 2. **The replaced object was not transparently replaceable.** After an object's lifetime ends and a new one is created in exactly the same storage, old pointers, references and names refer to the new object automatically, unless the old complete object was `const` or the object is a base class subobject; in those cases launder the pointer. C++17's wording also excluded classes with a `const` or reference member; C++20 dropped that condition, so the code example's `Entry` with a `const int id` is reachable through the old pointer under current rules. ## When std::start_lifetime_as is needed A different situation: storage already holds **bytes** that should be read as an object, but no object was ever created there. - Some operations implicitly create objects of **implicit-lifetime types** (scalars, arrays, aggregates whose destructor is not user-provided, and classes with a trivial eligible constructor and a trivial, non-deleted destructor) when that gives the program defined behaviour: `std::malloc`, `operator new`, `std::memcpy`, and beginning the lifetime of an `unsigned char` or `std::byte` array. That rule arrived in C++20 through P0593R6, as a defect report. - Bytes written into an existing buffer later, by `fread` into a `std::vector<char>` for instance, do not obviously fall under it. `std::start_lifetime_as<T>(p)` (C++23, P2590R2) states the intent: it implicitly creates a `T` at `p`, whose object representation is the storage's prior contents and whose value, for trivially copyable members, is what `std::bit_cast` would give. Preconditions: `T` is an implicit-lifetime type, and the region is allocated storage reachable through `p` and suitably aligned. It does not fix alignment, padding or byte order; a header read from a file still needs the layout checks. ```cpp // C++23; not compiled here: libc++ in Apple clang 21 does not provide it const FileHeader* h = std::start_lifetime_as<FileHeader>(buffer.data() + offset); ``` ## Choosing between them | Situation | Tool | |---|---| | you constructed the object with placement new | the pointer `new` returned | | storage-only wrapper must recompute the pointer | `std::launder(reinterpret_cast<T*>(storage))` | | replaced a `const` complete object or a base subobject | `std::launder` on the old pointer | | bytes already in a buffer should become a `T` | `std::start_lifetime_as<T>` (C++23) | | portable code, any standard | `std::memcpy` into a local `T` | ## Library support At the pinned releases, libstdc++ in GCC 16.2 declares `std::start_lifetime_as` and defines `__cpp_lib_start_lifetime_as` as 202207L in C++23 mode, while libc++'s C++23 status table lists P2590R2 with no implementation status and Apple clang 21 reported `no member named 'start_lifetime_as' in namespace 'std'`. Test the feature macro before relying on it, and keep the `memcpy` fallback.
- Why do std::optional-like wrappers need std::launder?They store an aligned byte buffer and construct the value into it with placement new, but to avoid an extra pointer member they recompute the value's address from the buffer on every access. A `reinterpret_cast` of the buffer still points to the bytes, not to the contained object, so the recomputed pointer is passed through `std::launder` to obtain one that points to the live object.
- Is std::start_lifetime_as the same as reinterpret_cast followed by a read?No. `reinterpret_cast` only converts a pointer and creates nothing, so reading through it requires an object of that type to exist already. `std::start_lifetime_as<T>` creates a `T` there, with its value taken from the existing bytes, and returns a pointer to it. It still requires an implicit-lifetime type and suitably aligned storage.
- A pointer with the right address always points to whatever object lives there.
- std::launder fixes misaligned pointers.
- std::start_lifetime_as converts the bytes to the platform's byte order.
- You must launder the pointer returned by placement new before using it.
- start_lifetime_as works for any type, including classes with a std::string member.
Assumes C++23 for std::start_lifetime_as (P2590R2), which libc++ in Apple clang 21 does not yet provide; std::launder is C++17 and implicit object creation is C++20.