AK: Add Optional::lazy_emplace(Callable)

This makes it possible to emplace using a given function instead of
passing constructor arguments.
This commit is contained in:
Ali Mohammad Pur 2022-12-10 19:36:32 +03:30 committed by Ali Mohammad Pur
parent 0ed9fe3864
commit 96b36203a2
Notes: sideshowbarker 2024-07-17 03:25:49 +09:00

View file

@ -200,6 +200,14 @@ public:
new (&m_storage) T(forward<Parameters>(parameters)...);
}
template<typename Callable>
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() &