Skip to content

Commit

Permalink
Implement LWG-3865 Sorting a range of pairs (#3476)
Browse files Browse the repository at this point in the history
Co-authored-by: Casey Carter <Casey@Carter.net>
  • Loading branch information
fsb4000 and CaseyCarter authored Feb 23, 2023
1 parent f91eba5 commit 9fb8168
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
31 changes: 16 additions & 15 deletions stl/inc/utility
Original file line number Diff line number Diff line change
Expand Up @@ -480,45 +480,46 @@ constexpr void swap(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Righ
}
#endif // _HAS_CXX23

_EXPORT_STD template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator==(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
_EXPORT_STD template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator==(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return _Left.first == _Right.first && _Left.second == _Right.second;
}

#ifdef __cpp_lib_concepts
_EXPORT_STD template <class _Ty1, class _Ty2>
_NODISCARD constexpr common_comparison_category_t<_Synth_three_way_result<_Ty1>, _Synth_three_way_result<_Ty2>>
operator<=>(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
_EXPORT_STD template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr common_comparison_category_t<_Synth_three_way_result<_Ty1, _Uty1>,
_Synth_three_way_result<_Ty2, _Uty2>>
operator<=>(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
if (auto _Result = _Synth_three_way{}(_Left.first, _Right.first); _Result != 0) {
return _Result;
}
return _Synth_three_way{}(_Left.second, _Right.second);
}
#else // ^^^ defined(__cpp_lib_concepts) / !defined(__cpp_lib_concepts) vvv
#if !_HAS_CXX20
template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator!=(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator!=(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return !(_Left == _Right);
}
#endif // !_HAS_CXX20

template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator<(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator<(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return _Left.first < _Right.first || (!(_Right.first < _Left.first) && _Left.second < _Right.second);
}

template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator>(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator>(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return _Right < _Left;
}

template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator<=(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator<=(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return !(_Right < _Left);
}

template <class _Ty1, class _Ty2>
_NODISCARD constexpr bool operator>=(const pair<_Ty1, _Ty2>& _Left, const pair<_Ty1, _Ty2>& _Right) {
template <class _Ty1, class _Ty2, class _Uty1, class _Uty2>
_NODISCARD constexpr bool operator>=(const pair<_Ty1, _Ty2>& _Left, const pair<_Uty1, _Uty2>& _Right) {
return !(_Left < _Right);
}
#endif // ^^^ !defined(__cpp_lib_concepts) ^^^
Expand Down
3 changes: 3 additions & 0 deletions tests/libcxx/expected_results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ std/ranges/range.access/rbegin.pass.cpp FAIL
std/ranges/range.access/rend.pass.cpp FAIL
std/ranges/range.access/size.pass.cpp FAIL

# libc++ doesn't implement LWG-3865 Sorting a range of pairs
std/utilities/tuple/tuple.tuple/tuple.apply/make_from_tuple.pass.cpp FAIL


# *** INTERACTIONS WITH CONTEST / C1XX THAT UPSTREAM LIKELY WON'T FIX ***
# Tracked by VSO-593630 "<filesystem> Enable libcxx filesystem tests"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ void my_swap(unique_ptr<incomplete>& lhs, unique_ptr<incomplete>& rhs) {
swap(lhs, rhs);
}

// also test LWG-3865 Sorting a range of pairs
constexpr bool test_lwg3865() {
const pair<int, int> a{1, 2};
const pair<long, long> b{1, 2};
const pair<long, long> c{2, 2};
assert(a == b);
assert(a != c);
assert(c >= a);
assert(b >= a);
assert(c > a);
assert(!(b > a));
assert(a < c);
assert(!(a < b));
assert(a <= c);
assert(a <= b);

return true;
}

int main() {
{
assert(g_objects == 0);
Expand Down Expand Up @@ -271,4 +290,7 @@ int main() {
};
(void) make_unique<unique_ptr<S>[]>(42);
}

test_lwg3865();
STATIC_ASSERT(test_lwg3865());
}

0 comments on commit 9fb8168

Please sign in to comment.