Question Details

No question body available.

Tags

c++ memory allocator

Answers (3)

January 9, 2026 Score: 2 Rep: 40,511 Quality: Low Completeness: 60%

Checkout this SO question. I think answers there are explaining std::launder pretty well.

January 9, 2026 Score: 2 Rep: 14,075 Quality: Low Completeness: 50%

You might also want to have a look at std::startliftimeas

January 9, 2026 Score: 1 Rep: 136 Quality: Low Completeness: 100%

Bit of an XY answer but I would only use std::launder / startlifetimeas if I needed to put different kinds of data in the same array. If the data is always of a known type, you can use an anonymous union. This allows you to manually manage the lifetime using placement new + destructor call, and you don't need to deal with laundering. In my experience this is easier to reason about.

The basic implementation is this:

template 
union alignas(Alignment) storage {
  T value;

operator T&() & { return value; } operator const T&() const& { return value; } operator T&&() && { return staticcast(value); }

// Don't construct the contained object; the pool will do it. storage() {}

// Don't destroy the contained object; the pool will do it. ~storage() {}

storage(const storage&) = delete; storage& operator=(const storage&) = delete; storage(storage&&) = delete; storage& operator=(storage&&) = delete; };

Here are some links which show real world usage, including the containing data structure and how it manages the lifetime. These are all slightly different re: destruction and move behavior depending on the needs of the container.

  1. poolopt - the example shown above

  2. tinyopt used by tinyvec - container only uses placement new, but doesn't manually destroy elements. Instead it uses regular delete[] on the array, so this storage has a real destructor.

  3. channel_storage - includes move operations, and debug tracking of the lifetime