From 5314f47b33d3bdfa0bf095fc51a8073b57173000 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sun, 2 Jan 2022 15:13:50 -0500 Subject: [PATCH] Hidden-friend tl::unexpected's comparison operators. These comparison operators were added in afa1b9dd. It would perhaps be better to simply remove them, at least `< <= > >=`, especially since `tl::expected` itself does not provide `< <= > >=`. --- include/tl/expected.hpp | 74 ++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index 31b130a..3b8240b 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -143,35 +143,29 @@ template class unexpected { TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); } constexpr const E &&value() const && { return std::move(m_val); } + friend constexpr bool operator==(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() == rhs.value(); + } + friend constexpr bool operator!=(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() != rhs.value(); + } + friend constexpr bool operator<(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() < rhs.value(); + } + friend constexpr bool operator<=(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() <= rhs.value(); + } + friend constexpr bool operator>(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() > rhs.value(); + } + friend constexpr bool operator>=(const unexpected &lhs, const unexpected &rhs) { + return lhs.value() >= rhs.value(); + } + private: E m_val; }; -template -constexpr bool operator==(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() == rhs.value(); -} -template -constexpr bool operator!=(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() != rhs.value(); -} -template -constexpr bool operator<(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() < rhs.value(); -} -template -constexpr bool operator<=(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() <= rhs.value(); -} -template -constexpr bool operator>(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() > rhs.value(); -} -template -constexpr bool operator>=(const unexpected &lhs, const unexpected &rhs) { - return lhs.value() >= rhs.value(); -} - template unexpected::type> make_unexpected(E &&e) { return unexpected::type>(std::forward(e)); @@ -1935,6 +1929,19 @@ class expected : private detail::expected_move_assign_base, "T must be move-constructible and convertible to from U&&"); return bool(*this) ? std::move(**this) : static_cast(std::forward(v)); } + + friend constexpr bool operator==(const expected &x, const unexpected &e) { + return x.has_value() ? false : x.error() == e.value(); + } + friend constexpr bool operator==(const unexpected &e, const expected &x) { + return x.has_value() ? false : x.error() == e.value(); + } + friend constexpr bool operator!=(const expected &x, const unexpected &e) { + return x.has_value() ? true : x.error() != e.value(); + } + friend constexpr bool operator!=(const unexpected &e, const expected &x) { + return x.has_value() ? true : x.error() != e.value(); + } }; namespace detail { @@ -2294,23 +2301,6 @@ constexpr bool operator!=(const U &v, const expected &x) { return x.has_value() ? *x != v : true; } -template -constexpr bool operator==(const expected &x, const unexpected &e) { - return x.has_value() ? false : x.error() == e.value(); -} -template -constexpr bool operator==(const unexpected &e, const expected &x) { - return x.has_value() ? false : x.error() == e.value(); -} -template -constexpr bool operator!=(const expected &x, const unexpected &e) { - return x.has_value() ? true : x.error() != e.value(); -} -template -constexpr bool operator!=(const unexpected &e, const expected &x) { - return x.has_value() ? true : x.error() != e.value(); -} - template ::value || std::is_move_constructible::value) &&