A deep dive into C++17's std::launder, explaining when and why it's needed to avoid undefined behavior when using placement new. Covers scenarios where the compiler's optimizer can make incorrect assumptions about pointer contents — particularly with const members or type-erased buffers — and shows how std::launder acts as an optimization fence. Also contrasts std::launder with reinterpret_cast, std::bit_cast, and std::start_lifetime_as in a summary table.

5m read timeFrom andreasfertig.com
Post cover image
Table of contents
When things may breakWhen things get more complicatedstd::launder to the rescue

Sort: