From e6cab7826a010b34670e825650167b0d975d5819 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Tue, 1 Aug 2023 23:36:27 +0200 Subject: c++: fix option example by laundering returned ptr --- src/development/c++/fwd-perfect.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/development/c++/fwd-perfect.cc b/src/development/c++/fwd-perfect.cc index 2fafc3e..15bf2b4 100644 --- a/src/development/c++/fwd-perfect.cc +++ b/src/development/c++/fwd-perfect.cc @@ -54,7 +54,11 @@ struct option { constexpr T& value() { assert(m_has_val); - return *reinterpret_cast(m_val); + // Placement new starts a new lifetime, launder pointer returned to the + // aligned storage. + // + // [1] https://en.cppreference.com/w/cpp/utility/launder + return *__builtin_launder(reinterpret_cast(m_val)); } private: -- cgit v1.2.3