std::launder

When to use std::launder?
Rule of thumb: If you use the pointer returned by std::construct_at or placement new, you usually don't need std::launder.
However, if you have a pre-existing pointer to storage (std::byte*, char*, etc.) that was just reused with placement new, you should use std::launder before accessing it as the new type.
A common HFT use case is object pools or ring buffers that reuse preallocated memory with placement new to avoid allocation latency. After reconstructing an object in reused storage, std::launder is used to obtain a pointer to the new live object and avoid undefined behavior caused by stale compiler lifetime assumptions.
struct Tick {
uint64_t ts;
double px;
};
// Pre-allocated memory
alignas(Tick) std::byte slot[sizeof(Tick)];
Producer thread:
// Placement new
new(slot) Tick{ts, px};
consumer thread:
// Tell the compiler that this address is now used for a new type
auto* tick = std::launder(reinterpret_cast<Tick*>(slot));
Use std::launder when you know something the compiler doesn’t.
Published at
2026-06-04 04:02:09 UTCEvent JSON
{
"id": "c23f33a1f9740445944efb2cbfbeccf8b721d7313bdd201f7f8af5838aa5bc17",
"pubkey": "d4dba46e95b5a924f957132a8fbb0072edd3c14709cdadd70d69bfbe6e2d3cef",
"created_at": 1780545729,
"kind": 30023,
"tags": [
[
"client",
"Yakihonne",
"31990:20986fb83e775d96d188ca5c9df10ce6d613e0eb7e5768a0f0b12b37cdac21b3:1700732875747"
],
[
"published_at",
"1780467418"
],
[
"d",
"OZ86Di8JK0EqpD9AMS6CR"
],
[
"image",
"https://image.nostr.build/9f3d47b2d200f6b249219d5b7938723f47be73f4dce7af16615be2e02cd2a072.png"
],
[
"title",
"std::launder"
],
[
"summary",
"When to use std::launder"
],
[
"t",
"C++"
]
],
"content": "**When to use std::launder?**\n\n**Rule of thumb:** If you use the pointer returned by `std::construct_at` or `placement new`, you usually don't need `std::launder`.\nHowever, if you have a pre-existing pointer to storage (`std::byte*`, `char*`, etc.) that was just reused with `placement new`, you should use `std::launder` before accessing it as the new type.\n\n\nA common HFT use case is object pools or ring buffers that reuse preallocated memory with `placement new` to avoid allocation latency. After reconstructing an object in reused storage, `std::launder` is used to obtain a pointer to the new live object and avoid undefined behavior caused by stale compiler lifetime assumptions.\n\n\n\n```cpp\nstruct Tick {\n uint64_t ts;\n double px;\n};\n\n// Pre-allocated memory\nalignas(Tick) std::byte slot[sizeof(Tick)];\n```\n\nProducer thread:\n\n```cpp\n// Placement new\nnew(slot) Tick{ts, px};\n```\n\n\nconsumer thread: \n\n```cpp\n// Tell the compiler that this address is now used for a new type\nauto* tick = std::launder(reinterpret_cast\u003cTick*\u003e(slot));\n```\n\n**Use `std::launder` when you know something the compiler doesn’t.**\n\n",
"sig": "631fa03550c79daad017e9ea3b5c9d1d72a4fae1edda979f48d111f8fae4460673c4fce16a696cd0c0182caaf27efafd5ca19e77be08453dfa94ad7fe8547734"
}