aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-08-01 23:36:27 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-08-01 23:36:27 +0200
commite6cab7826a010b34670e825650167b0d975d5819 (patch)
tree45ea0a085da64d6255a4011a781ba481e5e6161c
parent5778d4cb2db3081aa346f66cf3f3b7500b342dc6 (diff)
downloadnotes-e6cab7826a010b34670e825650167b0d975d5819.tar.gz
notes-e6cab7826a010b34670e825650167b0d975d5819.zip
c++: fix option example by laundering returned ptr
-rw-r--r--src/development/c++/fwd-perfect.cc6
1 files changed, 5 insertions, 1 deletions
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<T*>(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<T*>(m_val));
}
private: