Skip to content

Commit

Permalink
Hidden-friend tl::unexpected's comparison operators.
Browse files Browse the repository at this point in the history
These comparison operators were added in afa1b9d.
It would perhaps be better to simply remove them,
at least `< <= > >=`, especially since `tl::expected`
itself does not provide `< <= > >=`.
  • Loading branch information
Quuxplusone committed Jan 2, 2022
1 parent b285878 commit 5314f47
Showing 1 changed file with 32 additions and 42 deletions.
74 changes: 32 additions & 42 deletions include/tl/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,35 +143,29 @@ template <class E> 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 <class E>
constexpr bool operator==(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() == rhs.value();
}
template <class E>
constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() != rhs.value();
}
template <class E>
constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() < rhs.value();
}
template <class E>
constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() <= rhs.value();
}
template <class E>
constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() > rhs.value();
}
template <class E>
constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() >= rhs.value();
}

template <class E>
unexpected<typename std::decay<E>::type> make_unexpected(E &&e) {
return unexpected<typename std::decay<E>::type>(std::forward<E>(e));
Expand Down Expand Up @@ -1935,6 +1929,19 @@ class expected : private detail::expected_move_assign_base<T, E>,
"T must be move-constructible and convertible to from U&&");
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
}

friend constexpr bool operator==(const expected &x, const unexpected<E> &e) {
return x.has_value() ? false : x.error() == e.value();
}
friend constexpr bool operator==(const unexpected<E> &e, const expected &x) {
return x.has_value() ? false : x.error() == e.value();
}
friend constexpr bool operator!=(const expected &x, const unexpected<E> &e) {
return x.has_value() ? true : x.error() != e.value();
}
friend constexpr bool operator!=(const unexpected<E> &e, const expected &x) {
return x.has_value() ? true : x.error() != e.value();
}
};

namespace detail {
Expand Down Expand Up @@ -2294,23 +2301,6 @@ constexpr bool operator!=(const U &v, const expected<T, E> &x) {
return x.has_value() ? *x != v : true;
}

template <class T, class E>
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) {
return x.has_value() ? false : x.error() == e.value();
}
template <class T, class E>
constexpr bool operator==(const unexpected<E> &e, const expected<T, E> &x) {
return x.has_value() ? false : x.error() == e.value();
}
template <class T, class E>
constexpr bool operator!=(const expected<T, E> &x, const unexpected<E> &e) {
return x.has_value() ? true : x.error() != e.value();
}
template <class T, class E>
constexpr bool operator!=(const unexpected<E> &e, const expected<T, E> &x) {
return x.has_value() ? true : x.error() != e.value();
}

template <class T, class E,
detail::enable_if_t<(std::is_void<T>::value ||
std::is_move_constructible<T>::value) &&
Expand Down

0 comments on commit 5314f47

Please sign in to comment.