Skip to content

[libc++] P3379R1: Constrain std::expected equality operators #117664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcxx/docs/ReleaseNotes/20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Implemented Papers
- ``std::jthread`` and ``<stop_token>`` are not guarded behind ``-fexperimental-library`` anymore
- P2674R1: A trait for implicit lifetime types (`Github <https://github.com/llvm/llvm-project/issues/105259>`__)
- P0429R9: A Standard ``flat_map`` is partially implemented and ``flat_map`` is provided (`Github <https://github.com/llvm/llvm-project/issues/105190>`__)
- P3379R1: Constrain ``std::expected`` equality operators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The paper number is R0, please adjust all references in the PR

Suggested change
- P3379R1: Constrain ``std::expected`` equality operators
- P3379R0: Constrain ``std::expected`` equality operators

Copy link
Contributor

@jwakely jwakely Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a D3379R1 draft but I forgot to publish it, and we voted on R0. The only difference is that R1 said to add __cpp_lib_constrained_equality in <expected>. That addition got done editorially: cplusplus/draft#7449

It looks like this PR neither bumps the macro value nor adds the macro to <expected>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've submitted brevzin/sd6#28 to update SD-6.


Improvements and New Features
-----------------------------
Expand Down
32 changes: 32 additions & 0 deletions libcxx/include/__expected/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -1139,7 +1140,15 @@ class expected : private __expected_base<_Tp, _Err> {

// [expected.object.eq], equality operators
template <class _T2, class _E2>
# 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<bool>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convertible_to concept is stricter than the plain "(implicitly) convertible to". The latter can be expressed by __is_core_convertible in <__type_traits/is_core_convertible.h>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can have the following.

Internal concept __core_convertible_to in the <__concepts/core_convertible_to.h> internal header

#ifndef _LIBCPP___CONCEPTS_CORE_CONVERTIBLE_TO_H
#define _LIBCPP___CONCEPTS_CORE_CONVERTIBLE_TO_H

#include <__config>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 20

// [conv.general]/3 says "E is convertible to T" whenever "T t=E;" is well-formed.
// We can't test for that, but we can test implicit convertibility by passing it
// to a function. Unlike std::convertible_to, __core_convertible_to doesn't test
// static_cast or handle cv void, while accepting move-only types.

template <class _Tp, class _Up>
concept __core_convertible_to = requires {
  // rejects function and array types which are adjusted to pointer types in parameter lists
  static_cast<_Up (*)()>(nullptr)();
  static_cast<void (*)(_Up)>(nullptr)(static_cast<_Tp (*)()>(nullptr)());
};

#endif

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___CONCEPTS_CORE_CONVERTIBLE_TO_H

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for writing this! I'll take a look.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like to draft a patch for this header if you have time? I can rebase and use your header. This will ensure that you receive credit for your work! Otherwise, I can include it in this patch myself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we used __boolean_testable to implement the constraints for operator== in std::reference_wrapper. (https://github.com/llvm/llvm-project/blob/main/libcxx/include/__functional/reference_wrapper.h#L75-L81)
Should we consider making them consistent?

{ __err == __e2 } -> convertible_to<bool>;
})
# 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;
Expand All @@ -1153,11 +1162,22 @@ class expected : private __expected_base<_Tp, _Err> {
}

template <class _T2>
# if _LIBCPP_STD_VER >= 26
requires(!__is_std_expected<_T2>::value &&
requires(const _Tp& __tp, const _T2& __t2) {
{ __tp == __t2 } -> convertible_to<bool>;
})
# endif
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const _T2& __v) {
return __x.__has_val() && static_cast<bool>(__x.__val() == __v);
}

template <class _E2>
# if _LIBCPP_STD_VER >= 26
requires requires(const _Err& __err, const _E2& __e2) {
{ __err == __e2 } -> convertible_to<bool>;
}
# endif
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __e) {
return !__x.__has_val() && static_cast<bool>(__x.__unex() == __e.error());
}
Expand Down Expand Up @@ -1850,7 +1870,14 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {

// [expected.void.eq], equality operators
template <class _T2, class _E2>
# if _LIBCPP_STD_VER >= 26
requires(is_void_v<_T2> &&
requires(const _Err& __err, const _E2& __e2) {
{ __err == __e2 } -> convertible_to<bool>;
})
# 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;
Expand All @@ -1860,6 +1887,11 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {
}

template <class _E2>
# if _LIBCPP_STD_VER >= 26
requires requires(const _Err& __err, const _E2& __e2) {
{ __err == __e2 } -> convertible_to<bool>;
}
# endif
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const expected& __x, const unexpected<_E2>& __y) {
return !__x.__has_val() && static_cast<bool>(__x.__unex() == __y.error());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
template <class T1, class T2>
concept CanCompare = requires(T1 t1, T2 t2) { t1 == t2; };

struct Foo{};
static_assert(!CanCompare<Foo, Foo>);
struct NonComparable {};
static_assert(!CanCompare<NonComparable, NonComparable>);

static_assert(CanCompare<std::expected<int, int>, std::expected<int, int>>);
static_assert(CanCompare<std::expected<int, int>, std::expected<short, short>>);

// Note this is true because other overloads are unconstrained
static_assert(CanCompare<std::expected<int, int>, std::expected<void, int>>);
#if _LIBCPP_STD_VER >= 26
static_assert(!CanCompare<std::expected<int, int>, std::expected<void, int>>);
static_assert(!CanCompare<std::expected<NonComparable, int>, std::expected<NonComparable, int>>);
static_assert(!CanCompare<std::expected<int, NonComparable>, std::expected<int, NonComparable>>);
static_assert(!CanCompare<std::expected<NonComparable, NonComparable>, std::expected<NonComparable, NonComparable>>);
#endif

constexpr bool test() {
// x.has_value() && y.has_value()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
template <class T1, class T2>
concept CanCompare = requires(T1 t1, T2 t2) { t1 == t2; };

struct Foo{};
static_assert(!CanCompare<Foo, Foo>);
struct NonComparable {};
static_assert(!CanCompare<NonComparable, NonComparable>);

static_assert(CanCompare<std::expected<void, int>, std::expected<void, int>>);
static_assert(CanCompare<std::expected<void, int>, std::expected<void, short>>);

// Note this is true because other overloads in expected<non-void> are unconstrained
static_assert(CanCompare<std::expected<void, int>, std::expected<int, int>>);
#if _LIBCPP_STD_VER >= 26
static_assert(!CanCompare<std::expected<void, int>, std::expected<int, int>>);
static_assert(!CanCompare<std::expected<void, NonComparable>, std::expected<void, NonComparable>>);
#endif

constexpr bool test() {
// x.has_value() && y.has_value()
Expand Down
Loading