diff --git a/optional.hpp b/optional.hpp index 54bf141..37852ca 100644 --- a/optional.hpp +++ b/optional.hpp @@ -147,7 +147,7 @@ namespace tl { if (lhs.has_value()) return true; - return lhs.value() == rhs.value(); + return *lhs == *rhs; } template inline constexpr bool operator!=(const optional& lhs, const optional& rhs) { @@ -156,7 +156,7 @@ namespace tl { if (lhs.has_value()) return false; - return lhs.value() != rhs.value(); + return *lhs != *rhs; } template inline constexpr bool operator<(const optional& lhs, const optional& rhs) { @@ -165,7 +165,7 @@ namespace tl { if (!lhs.has_value()) return true; - return lhs.value() < rhs.value(); + return *lhs < *rhs; } template inline constexpr bool operator>(const optional& lhs, const optional& rhs) { @@ -174,7 +174,7 @@ namespace tl { if (!rhs.has_value()) return true; - return lhs.value() > rhs.value(); + return *lhs > *rhs; } template inline constexpr bool operator<=(const optional& lhs, const optional& rhs) { @@ -183,7 +183,7 @@ namespace tl { if (!rhs.has_value()) return false; - return lhs.value() <= rhs.value(); + return *lhs <= *rhs; } template inline constexpr bool operator>=(const optional& lhs, const optional& rhs) { @@ -192,7 +192,7 @@ namespace tl { if (!lhs.has_value()) return false; - return lhs.value() >= rhs.value(); + return *lhs >= *rhs; } // [optional.nullops], comparison with nullopt @@ -238,37 +238,37 @@ namespace tl { return lhs.has_value() ? *lhs == rhs : false; } template inline constexpr bool operator==(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs == rhs.value() : false; + return rhs.has_value() ? lhs == *rhs : false; } template inline constexpr bool operator!=(const optional& lhs, const U& rhs) { - return lhs.has_value() ? lhs.value() != lhs : true; + return lhs.has_value() ? *lhs != lhs : true; } template inline constexpr bool operator!=(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs != rhs.value() : true; + return rhs.has_value() ? lhs != *rhs : true; } template inline constexpr bool operator<(const optional& lhs, const U& rhs) { - return lhs.has_value() ? lhs.value() < lhs : true; + return lhs.has_value() ? *lhs < lhs : true; } template inline constexpr bool operator<(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs < rhs.value() : false; + return rhs.has_value() ? lhs < *rhs : false; } template inline constexpr bool operator<=(const optional& lhs, const U& rhs) { - return lhs.has_value() ? lhs.value() <= lhs : true; + return lhs.has_value() ? *lhs <= lhs : true; } template inline constexpr bool operator<=(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs <= rhs.value() : false; + return rhs.has_value() ? lhs <= *rhs : false; } template inline constexpr bool operator>(const optional& lhs, const U& rhs) { - return lhs.has_value() ? lhs.value() > lhs : false; + return lhs.has_value() ? *lhs > lhs : false; } template inline constexpr bool operator>(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs > rhs.value() : true; + return rhs.has_value() ? lhs > *rhs : true; } template inline constexpr bool operator>=(const optional& lhs, const U& rhs) { - return lhs.has_value() ? lhs.value() >= lhs : false; + return lhs.has_value() ? *lhs >= lhs : false; } template inline constexpr bool operator>=(const U& lhs, const optional& rhs) { - return rhs.has_value() ? lhs >= rhs.value() : true; + return rhs.has_value() ? lhs >= *rhs : true; } @@ -302,7 +302,7 @@ namespace std { if (!o.has_value()) return 0; - return hash>()(o.value()); + return hash>()(*o); } }; } @@ -356,14 +356,15 @@ namespace tl { constexpr optional(const optional& rhs) { if (rhs.has_value()) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (rhs.value()); + new (std::addressof(this->m_value)) T (*rhs); } } - template ::value>* = nullptr> + + // TODO conditionally disable constexpr optional(optional&& rhs) { if (rhs.has_value()) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (std::move(rhs.value())); + new (std::addressof(this->m_value)) T (std::move(*rhs)); } } template @@ -398,21 +399,21 @@ namespace tl { enable_if_t::value>* = nullptr> optional(const optional& rhs) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (rhs.value()); + new (std::addressof(this->m_value)) T (*rhs); } template * = nullptr, enable_if_t::value>* = nullptr> optional(const optional& rhs) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (rhs.value()); + new (std::addressof(this->m_value)) T (*rhs); } template * = nullptr, enable_if_t::value>* = nullptr> optional(optional&& rhs) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (std::move(rhs.value())); + new (std::addressof(this->m_value)) T (std::move(*rhs)); } @@ -420,7 +421,7 @@ namespace tl { enable_if_t::value>* = nullptr> explicit optional(optional&& rhs) { this->m_has_value = true; - new (std::addressof(this->m_value)) T (std::move(rhs.value())); + new (std::addressof(this->m_value)) T (std::move(*rhs)); } // [optional.dtor], destructor @@ -543,7 +544,7 @@ namespace tl { if (has_value()) { if (rhs.has_value()) { using std::swap; - swap(value(), rhs.value()); + swap(**this, *rhs); } else { new (&rhs.m_value) T (std::move(this->m_value)); @@ -600,12 +601,12 @@ namespace tl { template constexpr T value_or(U&& u) const& { static_assert(std::is_copy_constructible::value && std::is_convertible::value, "T must be copy constructible and convertible from U"); - return has_value() ? value() : static_cast(std::forward(u)); + return has_value() ? **this : static_cast(std::forward(u)); } template constexpr T value_or(U&& u) && { static_assert(std::is_move_constructible::value && std::is_convertible::value, "T must be move constructible and convertible from U"); - return has_value() ? value() : static_cast(std::forward(u)); + return has_value() ? **this : static_cast(std::forward(u)); } // [optional.mod], modifiers