[libcxx-commits] [libcxx] [libc++] [string_view] Remove operators made redundant by C++20 (PR #66206)

Amirreza Ashouri via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 14 00:47:20 PDT 2023


https://github.com/AMP999 updated https://github.com/llvm/llvm-project/pull/66206:

>From a5d1bd952bb94bff400dcab094e92477aead15d8 Mon Sep 17 00:00:00 2001
From: Amirreza Ashouri <ar.ashouri999 at gmail.com>
Date: Wed, 13 Sep 2023 15:46:09 +0330
Subject: [PATCH] [libc++] [string_view] Remove operators made redundant by
 C++20

Thanks to Giuseppe D'Angelo for pointing this out on the cpplang Slack!

The example implementation here...
https://eel.is/c++draft/string.view.comparison#example-1
...was necessary when it was written, in C++17, but in C++20
we don't need that complexity anymore, because of the reversed
candidates that are synthesized by the compiler.
---
 libcxx/include/string_view | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 3149fe250578fb2..9f5c0fd4c45a266 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -776,7 +776,35 @@ template <ranges::contiguous_range _Range>
 #endif
 
 // [string.view.comparison]
+
+#if _LIBCPP_STD_VER >= 20
+
+template<class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI constexpr
+bool operator==(basic_string_view<_CharT, _Traits> __lhs,
+                type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
+    if (__lhs.size() != __rhs.size()) return false;
+    return __lhs.compare(__rhs) == 0;
+}
+
+template <class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
+    basic_string_view<_CharT, _Traits> __lhs, type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
+    if constexpr (requires { typename _Traits::comparison_category; }) {
+        // [string.view]/4
+        static_assert(
+            __comparison_category<typename _Traits::comparison_category>,
+            "return type is not a comparison category type");
+        return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
+    } else {
+        return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
+    }
+}
+
+#else
+
 // operator ==
+
 template<class _CharT, class _Traits>
 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
 bool operator==(basic_string_view<_CharT, _Traits> __lhs,



More information about the libcxx-commits mailing list