diff --git a/deps/ox/src/ox/std/optional.hpp b/deps/ox/src/ox/std/optional.hpp index e1e7ab81..b8383cc0 100644 --- a/deps/ox/src/ox/std/optional.hpp +++ b/deps/ox/src/ox/std/optional.hpp @@ -48,6 +48,12 @@ class Optional { } } + constexpr ~Optional() { + if (m_ptr) { + m_ptr->~T(); + } + } + template constexpr U *get() noexcept { return m_ptr; @@ -86,12 +92,13 @@ class Optional { if (this == &other) { return *this; } - if (m_ptr) { - m_ptr->~T(); - } if (other.m_ptr) { - m_ptr = new(m_data) T(*other.m_ptr); - } else { + if (!m_ptr) { + emplace(); + } + *m_ptr = *other.m_ptr; + } else if (m_ptr) { + m_ptr->~T(); m_ptr = nullptr; } return *this; @@ -101,12 +108,13 @@ class Optional { if (this == &other) { return *this; } - if (m_ptr) { - m_ptr->~T(); - } if (other.m_ptr) { - m_ptr = new(m_data) T(std::move(*other.m_ptr)); - } else { + if (!m_ptr) { + emplace(); + } + *m_ptr = std::move(*other.m_ptr); + } else if (m_ptr) { + m_ptr->~T(); m_ptr = nullptr; } return *this; @@ -114,14 +122,14 @@ class Optional { template constexpr T &emplace(Args &&...args) { - m_ptr = new (m_data) T(ox::forward(args)...); + m_ptr = std::construct_at(m_ptr, ox::forward(args)...); return *m_ptr; } template constexpr T &emplace_subclass(Args &&...args) { static_assert(sizeof(U) <= buffSize, "Subclass is too large for this Optional"); - m_ptr = new (m_data) U(ox::forward(args)...); + m_ptr = std::construct_at(m_ptr, ox::forward(args)...); return *m_ptr; }