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

Implement LWG-3950 std::basic_string_view comparison operators are overspecified #4510

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 31 additions & 35 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1643,27 +1643,53 @@ namespace ranges {
} // namespace ranges
#endif // _HAS_CXX20

#if _HAS_CXX20
_EXPORT_STD template <class _Elem, class _Traits>
_NODISCARD constexpr bool operator==(const basic_string_view<_Elem, _Traits> _Lhs,
const type_identity_t<basic_string_view<_Elem, _Traits>> _Rhs) noexcept {
return _Lhs._Equal(_Rhs);
}

template <class _Traits, class = void>
struct _Get_comparison_category {
using type = weak_ordering;
};

template <class _Traits>
struct _Get_comparison_category<_Traits, void_t<typename _Traits::comparison_category>> {
using type = _Traits::comparison_category;

static_assert(_Is_any_of_v<type, partial_ordering, weak_ordering, strong_ordering>,
"N4971 [string.view.comparison]/4: Mandates: R denotes a comparison category type.");
};

template <class _Traits>
using _Get_comparison_category_t = _Get_comparison_category<_Traits>::type;

_EXPORT_STD template <class _Elem, class _Traits>
_NODISCARD constexpr _Get_comparison_category_t<_Traits> operator<=>(const basic_string_view<_Elem, _Traits> _Lhs,
const type_identity_t<basic_string_view<_Elem, _Traits>> _Rhs) noexcept {
return static_cast<_Get_comparison_category_t<_Traits>>(_Lhs.compare(_Rhs) <=> 0);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _Elem, class _Traits>
_NODISCARD constexpr bool operator==(
const basic_string_view<_Elem, _Traits> _Lhs, const basic_string_view<_Elem, _Traits> _Rhs) noexcept {
return _Lhs._Equal(_Rhs);
}

#if !_HAS_CXX20
template <class _Elem, class _Traits, int = 1> // TRANSITION, VSO-409326
_NODISCARD constexpr bool operator==(
const _Identity_t<basic_string_view<_Elem, _Traits>> _Lhs, const basic_string_view<_Elem, _Traits> _Rhs) noexcept {
return _Lhs._Equal(_Rhs);
}
#endif // !_HAS_CXX20

_EXPORT_STD template <class _Elem, class _Traits, int = 2> // TRANSITION, VSO-409326
template <class _Elem, class _Traits, int = 2> // TRANSITION, VSO-409326
_NODISCARD constexpr bool operator==(
const basic_string_view<_Elem, _Traits> _Lhs, const _Identity_t<basic_string_view<_Elem, _Traits>> _Rhs) noexcept {
return _Lhs._Equal(_Rhs);
}

#if !_HAS_CXX20
template <class _Elem, class _Traits>
_NODISCARD constexpr bool operator!=(
const basic_string_view<_Elem, _Traits> _Lhs, const basic_string_view<_Elem, _Traits> _Rhs) noexcept {
Expand Down Expand Up @@ -1753,37 +1779,7 @@ _NODISCARD constexpr bool operator>=(
const basic_string_view<_Elem, _Traits> _Lhs, const _Identity_t<basic_string_view<_Elem, _Traits>> _Rhs) noexcept {
return _Lhs.compare(_Rhs) >= 0;
}
#endif // !_HAS_CXX20

#if _HAS_CXX20
template <class _Traits, class = void>
struct _Get_comparison_category {
using type = weak_ordering;
};

template <class _Traits>
struct _Get_comparison_category<_Traits, void_t<typename _Traits::comparison_category>> {
using type = _Traits::comparison_category;

static_assert(_Is_any_of_v<type, partial_ordering, weak_ordering, strong_ordering>,
"N4950 [string.view.comparison]/4: Mandates: R denotes a comparison category type.");
};

template <class _Traits>
using _Get_comparison_category_t = _Get_comparison_category<_Traits>::type;

_EXPORT_STD template <class _Elem, class _Traits>
_NODISCARD constexpr _Get_comparison_category_t<_Traits> operator<=>(
const basic_string_view<_Elem, _Traits> _Lhs, const basic_string_view<_Elem, _Traits> _Rhs) noexcept {
return static_cast<_Get_comparison_category_t<_Traits>>(_Lhs.compare(_Rhs) <=> 0);
}

_EXPORT_STD template <class _Elem, class _Traits, int = 2> // TRANSITION, VSO-409326
_NODISCARD constexpr _Get_comparison_category_t<_Traits> operator<=>(
const basic_string_view<_Elem, _Traits> _Lhs, const _Identity_t<basic_string_view<_Elem, _Traits>> _Rhs) noexcept {
return static_cast<_Get_comparison_category_t<_Traits>>(_Lhs.compare(_Rhs) <=> 0);
}
#endif // _HAS_CXX20
#endif // ^^^ !_HAS_CXX20 ^^^

_EXPORT_STD using string_view = basic_string_view<char>;
#ifdef __cpp_lib_char8_t
Expand Down
73 changes: 73 additions & 0 deletions tests/std/tests/P0220R1_string_view/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,79 @@ void test_C6510_warning() { // compile-only
(void) sv;
}

#if _HAS_CXX20
// LWG-3950 "std::basic_string_view comparison operators are overspecified"
namespace test_lwg_3950 {
template <class Traits>
struct get_string_comparison_category {
using type = weak_ordering;
};

template <class Traits>
requires requires { typename Traits::comparison_category; }
struct get_string_comparison_category<Traits> {
using type = Traits::comparison_category;

static_assert(disjunction_v<is_same<type, partial_ordering>, is_same<type, weak_ordering>,
is_same<type, strong_ordering>>);
};

template <class Traits>
using get_string_comparison_category_t = get_string_comparison_category<Traits>::type;

template <class Traits>
concept characterized_traits = requires { typename Traits::is_characterized; };

#ifdef __clang__ // TRANSITION, LLVM-75404
#define CONST_PARAM const
#else // ^^^ workaround / no workaround vvv
#define CONST_PARAM
#endif // ^^^ no workaround ^^^

template <class CharT, characterized_traits Traits>
constexpr bool operator==(CONST_PARAM basic_string_view<CharT, Traits> x,
CONST_PARAM type_identity_t<basic_string_view<CharT, Traits>> y) noexcept {
return x.size() == y.size() && x.compare(y) == 0;
}
template <class CharT, characterized_traits Traits>
constexpr get_string_comparison_category_t<Traits> operator<=>(CONST_PARAM basic_string_view<CharT, Traits> x,
CONST_PARAM type_identity_t<basic_string_view<CharT, Traits>> y) noexcept {
return static_cast<get_string_comparison_category_t<Traits>>(x.compare(y) <=> 0);
}

template <class CharT>
struct test_traits : char_traits<CharT> {
using is_characterized = void;
};

using test_string_view = basic_string_view<char, test_traits<char>>;

static_assert(test_string_view{} == test_string_view{});
static_assert(!(test_string_view{} != test_string_view{}));
static_assert(!(test_string_view{} < test_string_view{}));
static_assert(!(test_string_view{} > test_string_view{}));
static_assert(test_string_view{} <= test_string_view{});
static_assert(test_string_view{} >= test_string_view{});
static_assert(test_string_view{} <=> test_string_view{} == strong_ordering::equal);

static_assert(test_string_view{} == "");
static_assert(!(test_string_view{} != ""));
static_assert(!(test_string_view{} < ""));
static_assert(!(test_string_view{} > ""));
static_assert(test_string_view{} <= "");
static_assert(test_string_view{} >= "");
static_assert(test_string_view{} <=> "" == strong_ordering::equal);

static_assert("" == test_string_view{});
static_assert(!("" != test_string_view{}));
static_assert(!("" < test_string_view{}));
static_assert(!("" > test_string_view{}));
static_assert("" <= test_string_view{});
static_assert("" >= test_string_view{});
static_assert("" <=> test_string_view{} == strong_ordering::equal);
} // namespace test_lwg_3950
#endif // _HAS_CXX20

int main() {
test_case_default_constructor();
test_case_ntcts_constructor();
Expand Down
Loading