From 96b36203a285baa942e2ca8e4e2d957879d00ba8 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sat, 10 Dec 2022 19:36:32 +0330 Subject: [PATCH] AK: Add Optional::lazy_emplace(Callable) This makes it possible to emplace using a given function instead of passing constructor arguments. --- AK/Optional.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/AK/Optional.h b/AK/Optional.h index 08f659dce4..6acf64f384 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -200,6 +200,14 @@ public: new (&m_storage) T(forward(parameters)...); } + template + ALWAYS_INLINE void lazy_emplace(Callable callable) + { + clear(); + m_has_value = true; + new (&m_storage) T { callable() }; + } + [[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_has_value; } [[nodiscard]] ALWAYS_INLINE T& value() &