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-3865 Sorting a range of pairs #3476

Merged
merged 5 commits into from
Feb 23, 2023
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
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) {
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
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());
}