[libcxx-commits] [libcxx] [libc++] Implement comparison operators for `tuple` added in C++23 (PR #148799)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 18 08:15:47 PDT 2025


================
@@ -288,6 +296,66 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #  ifndef _LIBCPP_CXX03_LANG
 
+template <size_t _Ip, class _Tp, class _Up>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __tuple_compare_equal(const _Tp& __x, const _Up& __y) {
+  if constexpr (_Ip == 0)
+    return true;
+  else
+    return std::__tuple_compare_equal<_Ip - 1>(__x, __y) && std::get<_Ip - 1>(__x) == std::get<_Ip - 1>(__y);
+}
+
+#    if _LIBCPP_STD_VER >= 26
+template <class _Tp, class _Up, class _IndexSeq>
+inline constexpr bool __can_tuple_compare_equal_impl = false;
----------------
ldionne wrote:

I think you can simplify this a bit by doing this instead:

```c++
template <class _Tp, class _Up, class _IndexSeq = make_index_sequence<tuple_size_v<_Tp>>>
inline constexpr bool __can_tuple_compare_equal = false;


template <class _Tp, class _Up, size_t... _Is>
   requires(tuple_size_v<_Tp> == tuple_size_v<_Up>)
inline constexpr bool __can_tuple_compare_equal<_Tp, _Up, index_sequence<_Is...>> =
  __all<requires(const tuple_element_t<_Is, _Tp>& __t, const tuple_element_t<_Is, _Up>& __u) {
    { __t == __u } -> __boolean_testable;
  }...>::value;
```

I think this lets you get rid of the `_impl` layer.

https://github.com/llvm/llvm-project/pull/148799


More information about the libcxx-commits mailing list