[ox/std] Fix various issues with Optional copy and move constructors

This commit is contained in:
Gary Talent 2023-02-19 16:48:35 -06:00
parent edd21017d3
commit 1767821161

View File

@ -32,16 +32,13 @@ class Optional {
constexpr Optional(const Optional &other) { constexpr Optional(const Optional &other) {
if (other.m_ptr) { if (other.m_ptr) {
m_ptr = new(m_data.data()) T(*other.m_ptr); m_ptr = std::construct_at<T>(m_data.data(), *other.m_ptr);
} }
} }
constexpr Optional(const Optional &&other) noexcept { constexpr Optional(Optional &&other) noexcept {
if (m_ptr) {
m_ptr->~T();
}
if (other.m_ptr) { if (other.m_ptr) {
m_ptr = new(m_data.data()) T(std::move(*other.m_ptr)); m_ptr = std::construct_at<T>(m_data.data(), std::move(*other.m_ptr));
} }
} }
@ -100,7 +97,7 @@ class Optional {
emplace(); emplace();
} }
*m_ptr = *other.m_ptr; *m_ptr = *other.m_ptr;
} else if (m_ptr) { } else {
reset(); reset();
} }
return *this; return *this;
@ -115,7 +112,7 @@ class Optional {
emplace(); emplace();
} }
*m_ptr = std::move(*other.m_ptr); *m_ptr = std::move(*other.m_ptr);
} else if (m_ptr) { } else {
reset(); reset();
} }
return *this; return *this;
@ -153,7 +150,7 @@ class Optional {
return m_ptr; return m_ptr;
} }
constexpr void reset() noexcept { constexpr void reset() {
if (std::is_constant_evaluated()) { if (std::is_constant_evaluated()) {
ox::safeDelete(m_ptr); ox::safeDelete(m_ptr);
} else if (has_value()) { } else if (has_value()) {