diff --git a/AK/Optional.h b/AK/Optional.h index a2d2a7c4e2..d214b17fb6 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -24,6 +24,9 @@ namespace AK { template class [[nodiscard]] Optional { + template + friend class Optional; + public: using ValueType = T; @@ -50,17 +53,31 @@ public: #endif : m_has_value(other.m_has_value) { - if (other.has_value()) { + if (other.has_value()) new (&m_storage) T(other.value()); - } } ALWAYS_INLINE Optional(Optional&& other) : m_has_value(other.m_has_value) { - if (other.has_value()) { + if (other.has_value()) + new (&m_storage) T(other.release_value()); + } + + template + requires(IsConstructible && !IsSpecializationOf && !IsSpecializationOf) ALWAYS_INLINE explicit Optional(Optional const& other) + : m_has_value(other.m_has_value) + { + if (other.has_value()) + new (&m_storage) T(other.value()); + } + + template + requires(IsConstructible && !IsSpecializationOf && !IsSpecializationOf) ALWAYS_INLINE explicit Optional(Optional&& other) + : m_has_value(other.m_has_value) + { + if (other.has_value()) new (&m_storage) T(other.release_value()); - } } template