-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++][NFC] Simplify the implementation of string and string_views operator== #117184
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -722,16 +722,19 @@ basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>; | |
|
||
// [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 { | ||
// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details. | ||
// This applies to the other sufficient overloads below for the other comparison operators. | ||
template <class _CharT, class _Traits, int = 1> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI 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; | ||
} | ||
|
||
# if _LIBCPP_STD_VER >= 20 | ||
|
||
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 { | ||
|
@@ -757,51 +760,32 @@ operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _ | |
return __lhs.compare(__rhs) == 0; | ||
} | ||
|
||
// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details. | ||
// This applies to the other sufficient overloads below for the other comparison operators. | ||
template <class _CharT, class _Traits, int = 1> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI 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, int = 2> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool | ||
operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs, | ||
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT { | ||
if (__lhs.size() != __rhs.size()) | ||
return false; | ||
return __lhs.compare(__rhs) == 0; | ||
return __lhs == __rhs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this not recursive? What did I miss? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This calls the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must be missing the point of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But how are these overloads not ambiguous with the one that takes two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gentle ping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that if you have more exact matches for the template parameter that is preferred over an argument that happens to have the same type. Consider e.g. template <class T, class U>
void func(T, U);
template <class T>
void func(T, T); Here the second overload is preferred, since it requires that the two arguments match exactly. I'm not 100% sure what the rule here is though. |
||
} | ||
|
||
// operator != | ||
template <class _CharT, class _Traits> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool | ||
operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT { | ||
if (__lhs.size() != __rhs.size()) | ||
return true; | ||
return __lhs.compare(__rhs) != 0; | ||
return !(__lhs == __rhs); | ||
} | ||
|
||
template <class _CharT, class _Traits, int = 1> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool | ||
operator!=(basic_string_view<_CharT, _Traits> __lhs, | ||
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT { | ||
if (__lhs.size() != __rhs.size()) | ||
return true; | ||
return __lhs.compare(__rhs) != 0; | ||
return !(__lhs == __rhs); | ||
} | ||
|
||
template <class _CharT, class _Traits, int = 2> | ||
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool | ||
operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs, | ||
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT { | ||
if (__lhs.size() != __rhs.size()) | ||
return true; | ||
return __lhs.compare(__rhs) != 0; | ||
return !(__lhs == __rhs); | ||
} | ||
|
||
// operator < | ||
|
Uh oh!
There was an error while loading. Please reload this page.