1313// template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
1414// class Proj = identity>
1515// requires sortable<I, Comp, Proj>
16- // I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
16+ // constexpr I // constexpr since C++26
17+ // ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); // since C++20
1718//
1819// template<random_access_range R, class Comp = ranges::less, class Proj = identity>
1920// requires sortable<iterator_t<R>, Comp, Proj>
20- // borrowed_iterator_t<R>
21+ // constexpr borrowed_iterator_t<R> // constexpr since C++26
2122// ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {}); // since C++20
2223
2324#include < algorithm>
@@ -57,7 +58,7 @@ static_assert(!HasStableSortR<UncheckedRange<int*>, BadComparator>);
5758static_assert (!HasStableSortR<UncheckedRange<const int *>>); // Doesn't satisfy `sortable`.
5859
5960template <class Iter , class Sent , std::size_t N>
60- void test_one (std::array<int , N> input, std::array<int , N> expected) {
61+ TEST_CONSTEXPR_CXX26 void test_one (std::array<int , N> input, std::array<int , N> expected) {
6162 { // (iterator, sentinel) overload.
6263 auto sorted = input;
6364 auto b = Iter (sorted.data ());
@@ -81,7 +82,7 @@ void test_one(std::array<int, N> input, std::array<int, N> expected) {
8182}
8283
8384template <class Iter , class Sent >
84- void test_iterators_2 () {
85+ TEST_CONSTEXPR_CXX26 void test_iterators_2 () {
8586 // Empty sequence.
8687 test_one<Iter, Sent, 0 >({}, {});
8788 // 1-element sequence.
@@ -105,25 +106,25 @@ void test_iterators_2() {
105106}
106107
107108template <class Iter >
108- void test_iterators_1 () {
109+ TEST_CONSTEXPR_CXX26 void test_iterators_1 () {
109110 test_iterators_2<Iter, Iter>();
110111 test_iterators_2<Iter, sentinel_wrapper<Iter>>();
111112}
112113
113- void test_iterators () {
114+ TEST_CONSTEXPR_CXX26 void test_iterators () {
114115 test_iterators_1<random_access_iterator<int *>>();
115116 test_iterators_1<contiguous_iterator<int *>>();
116117 test_iterators_1<int *>();
117118}
118119
119- void test () {
120+ TEST_CONSTEXPR_CXX26 bool test () {
120121 test_iterators ();
121122
122123 struct OrderedValue {
123124 int value;
124125 double original_order;
125126 bool operator ==(const OrderedValue&) const = default ;
126- auto operator <=>(const OrderedValue& rhs) const { return value <=> rhs.value ; }
127+ TEST_CONSTEXPR_CXX26 auto operator <=>(const OrderedValue& rhs) const { return value <=> rhs.value ; }
127128 };
128129
129130 { // The sort is stable (equivalent elements remain in the same order).
@@ -214,24 +215,24 @@ void test() {
214215 { // `std::invoke` is used in the implementation.
215216 struct S {
216217 int i;
217- S (int i_) : i(i_) {}
218+ TEST_CONSTEXPR_CXX26 S (int i_) : i(i_) {}
218219
219- bool comparator (const S& rhs) const { return i < rhs.i ; }
220- const S& projection () const { return *this ; }
220+ TEST_CONSTEXPR_CXX26 bool comparator (const S& rhs) const { return i < rhs.i ; }
221+ TEST_CONSTEXPR_CXX26 const S& projection () const { return *this ; }
221222
222223 bool operator ==(const S&) const = default ;
223224 };
224225
225226 {
226227 std::array in = {S{2 }, S{3 }, S{1 }};
227- auto last = std::ranges::stable_sort (in.begin (), in.end (), &S::comparator, &S::projection);
228+ auto last = std::ranges::stable_sort (in.begin (), in.end (), &S::comparator, &S::projection);
228229 assert ((in == std::array{S{1 }, S{2 }, S{3 }}));
229230 assert (last == in.end ());
230231 }
231232
232233 {
233234 std::array in = {S{2 }, S{3 }, S{1 }};
234- auto last = std::ranges::stable_sort (in, &S::comparator, &S::projection);
235+ auto last = std::ranges::stable_sort (in, &S::comparator, &S::projection);
235236 assert ((in == std::array{S{1 }, S{2 }, S{3 }}));
236237 assert (last == in.end ());
237238 }
@@ -242,8 +243,6 @@ void test() {
242243 std::ranges::stable_sort (std::array{1 , 2 , 3 });
243244 }
244245
245- // TODO: Enable the tests once the implementation switched to use iter_move/iter_swap
246- /*
247246 { // ProxyIterator
248247 {
249248 std::array in = {2 , 1 , 3 };
@@ -260,12 +259,15 @@ void test() {
260259 assert ((in == std::array{1 , 2 , 3 }));
261260 }
262261 }
263- */
262+
263+ return true ;
264264}
265265
266266int main (int , char **) {
267267 test ();
268- // Note: `stable_sort` is not `constexpr`.
268+ #if TEST_STD_VER >= 26
269+ static_assert (test ());
270+ #endif
269271
270272 return 0 ;
271273}
0 commit comments