Skip to content

Commit a807664

Browse files
committed
[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.
1 parent 7d7df7f commit a807664

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

libcxx/include/string_view

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,35 @@ template <ranges::contiguous_range _Range>
776776
#endif
777777

778778
// [string.view.comparison]
779+
780+
#if _LIBCPP_STD_VER >= 20
781+
782+
template<class _CharT, class _Traits>
783+
_LIBCPP_HIDE_FROM_ABI constexpr
784+
bool operator==(basic_string_view<_CharT, _Traits> __lhs,
785+
type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
786+
if (__lhs.size() != __rhs.size()) return false;
787+
return __lhs.compare(__rhs) == 0;
788+
}
789+
790+
template <class _CharT, class _Traits>
791+
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
792+
basic_string_view<_CharT, _Traits> __lhs, type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
793+
if constexpr (requires { typename _Traits::comparison_category; }) {
794+
// [string.view]/4
795+
static_assert(
796+
__comparison_category<typename _Traits::comparison_category>,
797+
"return type is not a comparison category type");
798+
return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
799+
} else {
800+
return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
801+
}
802+
}
803+
804+
#else
805+
779806
// operator ==
807+
780808
template<class _CharT, class _Traits>
781809
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
782810
bool operator==(basic_string_view<_CharT, _Traits> __lhs,
@@ -797,8 +825,6 @@ bool operator==(basic_string_view<_CharT, _Traits> __lhs,
797825
return __lhs.compare(__rhs) == 0;
798826
}
799827

800-
#if _LIBCPP_STD_VER < 20
801-
// This overload is automatically generated in C++20.
802828
template<class _CharT, class _Traits, int = 2>
803829
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_INLINE_VISIBILITY
804830
bool operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
@@ -807,41 +833,6 @@ bool operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
807833
if (__lhs.size() != __rhs.size()) return false;
808834
return __lhs.compare(__rhs) == 0;
809835
}
810-
#endif // _LIBCPP_STD_VER >= 20
811-
812-
// operator <=>
813-
814-
#if _LIBCPP_STD_VER >= 20
815-
816-
template <class _CharT, class _Traits>
817-
_LIBCPP_HIDE_FROM_ABI constexpr auto
818-
operator<=>(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) noexcept {
819-
if constexpr (requires { typename _Traits::comparison_category; }) {
820-
// [string.view]/4
821-
static_assert(
822-
__comparison_category<typename _Traits::comparison_category>,
823-
"return type is not a comparison category type");
824-
return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
825-
} else {
826-
return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
827-
}
828-
}
829-
830-
template <class _CharT, class _Traits, int = 1>
831-
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(
832-
basic_string_view<_CharT, _Traits> __lhs, type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
833-
if constexpr (requires { typename _Traits::comparison_category; }) {
834-
// [string.view]/4
835-
static_assert(
836-
__comparison_category<typename _Traits::comparison_category>,
837-
"return type is not a comparison category type");
838-
return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
839-
} else {
840-
return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
841-
}
842-
}
843-
844-
#else // _LIBCPP_STD_VER >= 20
845836

846837
// operator !=
847838
template<class _CharT, class _Traits>

0 commit comments

Comments
 (0)