[libcxx-commits] [libcxx] [libc++] P3379R1: Constrain `std::expected` equality operators (PR #117664)

Xiaoyang Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 27 22:16:43 PST 2024


https://github.com/xiaoyang-sde updated https://github.com/llvm/llvm-project/pull/117664

>From 002fcac983085257ad28dfa9b45d8a7493e7dccc Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Mon, 25 Nov 2024 23:26:19 -0500
Subject: [PATCH 1/2] [libc++] P3379R1: Constrain 'std::expected' equality
 operators

---
 libcxx/include/__expected/expected.h | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 3d3f11967ee746..7dc0248fcd72f9 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,11 @@ class expected : private __expected_base<_Tp, _Err> {
 
   // [expected.object.eq], equality operators
   template <class _T2, class _E2>
-    requires(!is_void_v<_T2>)
+    requires(!is_void_v<_T2> &&
+             requires(const _Tp& __tp, const _T2& __t2, const _Err& __err, const _E2& __e2) {
+               { __tp == __t2 } -> convertible_to<bool>;
+               { __err == __e2 } -> convertible_to<bool>;
+             })
   _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 +1158,18 @@ class expected : private __expected_base<_Tp, _Err> {
   }
 
   template <class _T2>
+    requires(!__is_std_expected<_T2>::value &&
+             requires(const _Tp& __tp, const _T2& __t2) {
+               { __tp == __t2 } -> convertible_to<bool>;
+             })
   _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>
+    requires requires(const _Err& __err, const _E2& __e2) {
+      { __err == __e2 } -> convertible_to<bool>;
+    }
   _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());
   }
@@ -1850,7 +1862,10 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {
 
   // [expected.void.eq], equality operators
   template <class _T2, class _E2>
-    requires is_void_v<_T2>
+    requires(is_void_v<_T2> &&
+             requires(const _Err& __err, const _E2& __e2) {
+               { __err == __e2 } -> convertible_to<bool>;
+             })
   _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 +1875,9 @@ class expected<_Tp, _Err> : private __expected_void_base<_Err> {
   }
 
   template <class _E2>
+    requires requires(const _Err& __err, const _E2& __e2) {
+      { __err == __e2 } -> convertible_to<bool>;
+    }
   _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());
   }

>From 408b3315450bf97e4b1c0e23f37dc9462c71578b Mon Sep 17 00:00:00 2001
From: Xiaoyang Liu <siujoeng.lau at gmail.com>
Date: Thu, 28 Nov 2024 01:16:30 -0500
Subject: [PATCH 2/2] [libc++] P3379R1: Constrain 'std::expected' equality
 operators

---
 libcxx/include/__expected/expected.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 7dc0248fcd72f9..ca9dcfb1569e8b 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -1140,11 +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>;
                { __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;
@@ -1158,18 +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());
   }
@@ -1862,10 +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;
@@ -1875,9 +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());
   }



More information about the libcxx-commits mailing list