diff --git a/libcxx/include/set b/libcxx/include/set --- a/libcxx/include/set +++ b/libcxx/include/set @@ -210,27 +210,31 @@ template bool operator< (const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator!=(const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator> (const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator>=(const set& x, - const set& y); + const set& y); // removed in C++20 template bool operator<=(const set& x, - const set& y); + const set& y); // removed in C++20 + +template + synth-three-way-result operator<=>(const set& x, + const set& y); // since C++20 // specialized algorithms: template @@ -435,27 +439,31 @@ template bool operator< (const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator!=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator> (const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator>=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 template bool operator<=(const multiset& x, - const multiset& y); + const multiset& y); // removed in C++20 + +template + synth-three-way-result operator<=>(const multiset& x, + const multiset& y); // since C++20 // specialized algorithms: template @@ -473,6 +481,7 @@ #include <__algorithm/equal.h> #include <__algorithm/lexicographical_compare.h> +#include <__algorithm/lexicographical_compare_three_way.h> #include <__assert> // all public C++ headers provide the assertion handler #include <__config> #include <__functional/is_transparent.h> @@ -982,6 +991,8 @@ return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); } +#if _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY bool @@ -1027,6 +1038,17 @@ return !(__y < __x); } +#else // _LIBCPP_STD_VER <= 17 + +template +_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key> +operator<=>(const set<_Key, _Allocator>& __x, const set<_Key, _Allocator>& __y) { + return std::lexicographical_compare_three_way( + __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>); +} + +#endif // _LIBCPP_STD_VER <= 17 + // specialized algorithms: template inline _LIBCPP_INLINE_VISIBILITY @@ -1518,6 +1540,8 @@ return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); } +#if _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY bool @@ -1563,6 +1587,17 @@ return !(__y < __x); } +#else // _LIBCPP_STD_VER <= 17 + +template +inline _LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Key> +operator<=>(const multiset<_Key, _Allocator>& __x, const multiset<_Key, _Allocator>& __y) { + return std::lexicographical_compare_three_way( + __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Key, _Key>); +} + +#endif // _LIBCPP_STD_VER <= 17 + template inline _LIBCPP_INLINE_VISIBILITY void diff --git a/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class multiset + +// template +// synth-three-way-result operator<=>(const multiset& x, +// const multiset& y); + +#include +#include + +#include "test_container_comparisons.h" + +int main(int, char**) { + assert(test_ordered_set_container_spaceship()); + // `std::multiset` is not constexpr, so no `static_assert` test here. + return 0; +} diff --git a/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.verify.cpp b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/multiset/multiset.nonmember/compare.three_way.verify.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class set + +// template +// synth-three-way-result operator<=>(const set& x, +// const set& y); + +#include + +#include "test_allocator.h" + +int main(int, char**) { + // Mismatching comparision functions + { + std::multiset> s1; + std::multiset> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + { + std::multiset> s1; + std::multiset> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + // Mismatching types + { + std::multiset s1; + std::multiset s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + // Mismatching allocators + { + std::multiset, std::allocator> s1; + std::multiset, test_allocator> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + + return 0; +} diff --git a/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class set + +// template +// synth-three-way-result operator<=>(const set& x, +// const set& y); + +#include +#include + +#include "test_container_comparisons.h" + +int main(int, char**) { + assert((test_ordered_set_container_spaceship())); + // `std::set` is not constexpr, so no `static_assert` test here. + return 0; +} diff --git a/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.verify.cpp b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/associative/set/set.nonmember/compare.three_way.verify.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// class set + +// template +// synth-three-way-result operator<=>(const set& x, +// const set& y); + +#include + +#include "test_allocator.h" + +int main(int, char**) { + // Mismatching comparision functions + { + std::set> s1; + std::set> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + { + std::set> s1; + std::set> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + // Mismatching types + { + std::set s1; + std::set s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + // Mismatching allocators + { + std::set, std::allocator> s1; + std::set, test_allocator> s2; + // expected-error@*:* 2 {{invalid operands to binary expression}} + s1 <=> s2; + s2 <=> s1; + } + + return 0; +} diff --git a/libcxx/test/support/test_comparisons.h b/libcxx/test/support/test_comparisons.h --- a/libcxx/test/support/test_comparisons.h +++ b/libcxx/test/support/test_comparisons.h @@ -241,7 +241,8 @@ } }; -#if TEST_STD_VER > 17 +#if TEST_STD_VER >= 20 + struct StrongOrder { int value; constexpr StrongOrder(int v) : value(v) {} @@ -254,11 +255,16 @@ friend std::weak_ordering operator<=>(WeakOrder, WeakOrder) = default; }; +constexpr auto UnorderableIntMin = std::numeric_limits::min(); +constexpr auto UnorderableIntMax = std::numeric_limits::max(); + struct PartialOrder { int value; constexpr PartialOrder(int v) : value(v) {} friend constexpr std::partial_ordering operator<=>(PartialOrder lhs, PartialOrder rhs) { - if (lhs.value == std::numeric_limits::min() || rhs.value == std::numeric_limits::min()) + if (lhs.value == UnorderableIntMin || rhs.value == UnorderableIntMin) + return std::partial_ordering::unordered; + if (lhs.value == UnorderableIntMax || rhs.value == UnorderableIntMax) return std::partial_ordering::unordered; return lhs.value <=> rhs.value; } diff --git a/libcxx/test/support/test_container_comparisons.h b/libcxx/test/support/test_container_comparisons.h --- a/libcxx/test/support/test_container_comparisons.h +++ b/libcxx/test/support/test_container_comparisons.h @@ -10,6 +10,8 @@ #ifndef TEST_CONTAINER_COMPARISONS #define TEST_CONTAINER_COMPARISONS +#include + #include "test_comparisons.h" // Implementation detail of `test_sequence_container_spaceship` @@ -53,7 +55,7 @@ } // Unordered if constexpr (std::is_same_v) { - Container l1{1, std::numeric_limits::min()}; + Container l1{1, UnorderableIntMin}; Container l2{1, 2}; assert(testOrder(l1, l2, Order::unordered)); } @@ -122,7 +124,7 @@ } // Unordered if constexpr (std::is_same_v) { - Container l1{{1, 1}, {2, std::numeric_limits::min()}}; + Container l1{{1, 1}, {2, UnorderableIntMin}}; Container l2{{1, 1}, {2, 2}}; assert(testOrder(l1, l2, Order::unordered)); } @@ -174,16 +176,16 @@ } // Unordered if constexpr (std::is_same_v) { - Container l1{{1, 1}, {2, std::numeric_limits::min()}, {2, 3}}; + Container l1{{1, 1}, {2, UnorderableIntMin}, {2, 3}}; Container l2{{1, 1}, {2, 2}, {2, 3}}; assert(testOrder(l1, l2, Order::unordered)); - Container l3{{1, 1}, {2, std::numeric_limits::min()}, {2, 3}}; + Container l3{{1, 1}, {2, UnorderableIntMin}, {2, 3}}; Container l4{{2, 3}, {2, 2}, {1, 1}}; assert(testOrder(l3, l4, Order::unordered)); } } -// Tests the `operator<=>` on ordered containers +// Tests the `operator<=>` on ordered map containers template