diff --git a/libcxx/docs/ReleaseNotes/20.rst b/libcxx/docs/ReleaseNotes/20.rst index a72fd2d5db9e2..dee43267e4501 100644 --- a/libcxx/docs/ReleaseNotes/20.rst +++ b/libcxx/docs/ReleaseNotes/20.rst @@ -45,6 +45,7 @@ Implemented Papers - ``std::jthread`` and ```` are not guarded behind ``-fexperimental-library`` anymore - P2674R1: A trait for implicit lifetime types (`Github `__) - P0429R9: A Standard ``flat_map`` is partially implemented and ``flat_map`` is provided (`Github `__) +- P3379R1: Constrain ``std::expected`` equality operators Improvements and New Features ----------------------------- diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h index 3d3f11967ee74..ca9dcfb1569e8 100644 --- a/libcxx/include/__expected/expected.h +++ b/libcxx/include/__expected/expected.h @@ -10,6 +10,7 @@ #define _LIBCPP___EXPECTED_EXPECTED_H #include <__assert> +#include <__concepts/convertible_to.h> #include <__config> #include <__expected/bad_expected_access.h> #include <__expected/unexpect.h> @@ -1139,7 +1140,15 @@ class expected : private __expected_base<_Tp, _Err> { // [expected.object.eq], equality operators template +# if _LIBCPP_STD_VER >= 26 + requires(!is_void_v<_T2> && + requires(const _Tp& __tp, const _T2& __t2, const _Err& __err, const _E2& __e2) { + { __tp == __t2 } -> convertible_to; + { __err == __e2 } -> convertible_to; + }) +# else requires(!is_void_v<_T2>) +# endif _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) { if (__x.__has_val() != __y.__has_val()) { return false; @@ -1153,11 +1162,22 @@ class expected : private __expected_base<_Tp, _Err> { } template +# if _LIBCPP_STD_VER >= 26 + requires(!__is_std_expected<_T2>::value && + requires(const _Tp& __tp, const _T2& __t2) { + { __tp == __t2 } -> convertible_to; + }) +# endif _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const _T2& __v) { return __x.__has_val() && static_cast(__x.__val() == __v); } template +# if _LIBCPP_STD_VER >= 26 + requires requires(const _Err& __err, const _E2& __e2) { + { __err == __e2 } -> convertible_to; + } +# endif _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) { return !__x.__has_val() && static_cast(__x.__unex() == __e.error()); } @@ -1850,7 +1870,14 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> { // [expected.void.eq], equality operators template +# if _LIBCPP_STD_VER >= 26 + requires(is_void_v<_T2> && + requires(const _Err& __err, const _E2& __e2) { + { __err == __e2 } -> convertible_to; + }) +# else requires is_void_v<_T2> +# endif _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const expected<_T2, _E2>& __y) { if (__x.__has_val() != __y.__has_val()) { return false; @@ -1860,6 +1887,11 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> { } template +# if _LIBCPP_STD_VER >= 26 + requires requires(const _Err& __err, const _E2& __e2) { + { __err == __e2 } -> convertible_to; + } +# endif _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) { return !__x.__has_val() && static_cast(__x.__unex() == __y.error()); } diff --git a/libcxx/test/std/utilities/expected/expected.expected/equality/equality.other_expected.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/equality/equality.other_expected.pass.cpp index 9325c6c61ad2d..3a044afe93728 100644 --- a/libcxx/test/std/utilities/expected/expected.expected/equality/equality.other_expected.pass.cpp +++ b/libcxx/test/std/utilities/expected/expected.expected/equality/equality.other_expected.pass.cpp @@ -23,14 +23,18 @@ template concept CanCompare = requires(T1 t1, T2 t2) { t1 == t2; }; -struct Foo{}; -static_assert(!CanCompare); +struct NonComparable {}; +static_assert(!CanCompare); static_assert(CanCompare, std::expected>); static_assert(CanCompare, std::expected>); -// Note this is true because other overloads are unconstrained -static_assert(CanCompare, std::expected>); +#if _LIBCPP_STD_VER >= 26 +static_assert(!CanCompare, std::expected>); +static_assert(!CanCompare, std::expected>); +static_assert(!CanCompare, std::expected>); +static_assert(!CanCompare, std::expected>); +#endif constexpr bool test() { // x.has_value() && y.has_value() diff --git a/libcxx/test/std/utilities/expected/expected.void/equality/equality.other_expected.pass.cpp b/libcxx/test/std/utilities/expected/expected.void/equality/equality.other_expected.pass.cpp index 8b24875586852..731368c4d2396 100644 --- a/libcxx/test/std/utilities/expected/expected.void/equality/equality.other_expected.pass.cpp +++ b/libcxx/test/std/utilities/expected/expected.void/equality/equality.other_expected.pass.cpp @@ -23,14 +23,16 @@ template concept CanCompare = requires(T1 t1, T2 t2) { t1 == t2; }; -struct Foo{}; -static_assert(!CanCompare); +struct NonComparable {}; +static_assert(!CanCompare); static_assert(CanCompare, std::expected>); static_assert(CanCompare, std::expected>); -// Note this is true because other overloads in expected are unconstrained -static_assert(CanCompare, std::expected>); +#if _LIBCPP_STD_VER >= 26 +static_assert(!CanCompare, std::expected>); +static_assert(!CanCompare, std::expected>); +#endif constexpr bool test() { // x.has_value() && y.has_value()