Skip to content
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

[libc++] [string_view] Remove operators made redundant by C++20 #66206

Merged
merged 1 commit into from
Sep 18, 2023

Conversation

AMP999
Copy link
Contributor

@AMP999 AMP999 commented Sep 13, 2023

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.

@AMP999 AMP999 requested a review from a team as a code owner September 13, 2023 13:05
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 13, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-libcxx

Changes 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.

Full diff: https://github.com/llvm/llvm-project/pull/66206.diff

1 Files Affected:

  • (modified) libcxx/include/string_view (+27-38)
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 3149fe250578fb2..42ebd87218099a9 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -776,7 +776,33 @@ template <ranges::contiguous_range _Range>
 #endif
 
 // [string.view.comparison]
-// operator ==
+
+#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
+
 template<class _CharT, class _Traits>
 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
 bool operator==(basic_string_view<_CharT, _Traits> __lhs,
@@ -797,8 +823,6 @@ bool operator==(basic_string_view<_CharT, _Traits> __lhs,
     return __lhs.compare(__rhs) == 0;
 }
 
-#if _LIBCPP_STD_VER < 20
-// This overload is automatically generated in C++20.
 template<class _CharT, class _Traits, int = 2>
 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
 bool operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
@@ -807,41 +831,6 @@ bool operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
     if (__lhs.size() != __rhs.size()) return false;
     return __lhs.compare(__rhs) == 0;
 }
-#endif // _LIBCPP_STD_VER >= 20
-
-// operator <=>
-
-#if _LIBCPP_STD_VER >= 20
-
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI constexpr auto
-operator<=>(basic_string_view<_CharT, _Traits> __lhs, 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);
-    }
-}
-
-template <class _CharT, class _Traits, int = 1>
-_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 //  _LIBCPP_STD_VER >= 20
 
 // operator !=
 template<class _CharT, class _Traits>

@ldionne ldionne requested a review from Zingam September 13, 2023 16:17
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

This seems reasonable but I'd like @Zingam to take a look since he's done a lot of our support for operator<=>.

libcxx/include/string_view Show resolved Hide resolved
libcxx/include/string_view Show resolved Hide resolved
libcxx/include/string_view Show resolved Hide resolved
@ldionne ldionne self-assigned this Sep 15, 2023
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.
@AMP999 AMP999 requested a review from a team as a code owner September 15, 2023 21:55
@ldionne ldionne merged commit aa8601d into llvm:main Sep 18, 2023
@ldionne
Copy link
Member

ldionne commented Sep 18, 2023

Thanks! FYI @AMP999 I see that your github setting are marked as hiding your email, which means that patches are being committed with a github-generated email address. Just a heads up in case that's not the behavior you want.

@AMP999
Copy link
Contributor Author

AMP999 commented Sep 18, 2023

You're welcome! Thank you for notifying me about the email!

ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…#66206)

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

The example implementation in 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.
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
…#66206)

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

The example implementation in 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.
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
…#66206)

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

The example implementation in 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.
ldionne added a commit to ldionne/llvm-project that referenced this pull request Apr 3, 2024
Justifications:
- LWG3950: Done in llvm#66206
- LWG3975: Wording changes only
- LWG4011: Wording changes only
- LWG4030: Wording changes only
- LWG4043: Wording changes only
- LWG3036 and P2875R4: We implemented neither, but the latter reverts
  the former, so now we implement both without doing anything!
ldionne added a commit that referenced this pull request Apr 3, 2024
Justifications:
- LWG3950: Done in #66206
- LWG3975: Wording changes only
- LWG4011: Wording changes only
- LWG4030: Wording changes only
- LWG4043: Wording changes only
- LWG3036 and P2875R4: We implemented neither, but the latter reverts
the former, so now we implement both without doing anything!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants