Skip to content

Commit

Permalink
Fix operator==() for void 'value' type (#51, thanks @Crzyrndm)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Nov 6, 2022
1 parent 031d1a5 commit 70db1b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 15 additions & 3 deletions include/nonstd/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,11 @@ class expected<void, E>

// x.x.4.6 expected<>: comparison operators

template< typename T1, typename E1, typename T2, typename E2 >
template< typename T1, typename E1, typename T2, typename E2
nsel_REQUIRES_T(
!std::is_void<T1>::value && !std::is_void<T2>::value
)
>
constexpr bool operator==( expected<T1,E1> const & x, expected<T2,E2> const & y )
{
return bool(x) != bool(y) ? false : bool(x) == false ? x.error() == y.error() : *x == *y;
Expand Down Expand Up @@ -2213,13 +2217,21 @@ constexpr bool operator>=( expected<T,E> const & x, expected<T,E> const & y )

// x.x.4.7 expected: comparison with T

template< typename T1, typename E1, typename T2 >
template< typename T1, typename E1, typename T2
nsel_REQUIRES_T(
!std::is_void<T1>::value
)
>
constexpr bool operator==( expected<T1,E1> const & x, T2 const & v )
{
return bool(x) ? *x == v : false;
}

template< typename T1, typename E1, typename T2 >
template< typename T1, typename E1, typename T2
nsel_REQUIRES_T(
!std::is_void<T1>::value
)
>
constexpr bool operator==(T2 const & v, expected<T1,E1> const & x )
{
return bool(x) ? v == *x : false;
Expand Down
18 changes: 18 additions & 0 deletions test/expected.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,24 @@ nonstd::unexpected_type<MyConstMemberNonMoveableObject> create_nonmoveable()

} // namespace issue_50

namespace issue_51 {

int compare_equal_with_expected_void()
{
auto ev1 = nonstd::expected<void, int>{};
auto ev2 = nonstd::expected<void, int>{};
return ev1 == ev2;
}

int compare_not_equal_with_expected_void()
{
auto ev1 = nonstd::expected<void, int>{};
auto ev2 = nonstd::expected<void, int>{};
return ev1 != ev2;
}

} // namespace issue_51

// -----------------------------------------------------------------------
// using as optional

Expand Down

0 comments on commit 70db1b5

Please sign in to comment.