Skip to content

Commit

Permalink
[libc++] Fix unexpected heterogeneous comparison
Browse files Browse the repository at this point in the history
Currently, libc++ incorrectly rejects heterogeneous comparison of
`unexpected`, because the `operator==` is only a hidden friend of
`unexpected<_Err>` but not of `unexpected<_Err2>`. We need to call
`error()` member function on `__y`.
  • Loading branch information
frederick-vs-ja committed Nov 7, 2024
1 parent 30d8000 commit 921d491
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libcxx/include/__expected/unexpected.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class unexpected {

template <class _Err2>
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const unexpected& __x, const unexpected<_Err2>& __y) {
return __x.__unex_ == __y.__unex_;
return __x.__unex_ == __y.error();
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@
struct Error{
int i;
friend constexpr bool operator==(const Error&, const Error&) = default;
friend constexpr bool operator==(const Error& lhs, int rhs) noexcept {
return lhs.i == rhs;
}
};

constexpr bool test() {
std::unexpected<Error> unex1(Error{2});
std::unexpected<Error> unex2(Error{3});
std::unexpected<Error> unex3(Error{2});

assert(unex1 == unex3);
assert(unex1 != unex2);
assert(unex2 != unex3);

std::unexpected<int> unex_i1(1);
std::unexpected<int> unex_i2(2);

assert(unex1 != unex_i1);
assert(unex_i1 != unex1);
assert(unex1 == unex_i2);
assert(unex_i2 == unex1);

return true;
}

Expand Down

0 comments on commit 921d491

Please sign in to comment.