On Fri, 04 Dec 2026 at 02:15 +0000, Waffl3x wrote:
I also eyeballed the rest of the views that conditionally use
operator<=>, the rest seem fine. I suspect the test case I added is not
satisfactory. In particular, it is not enabled for C++20 mode but I did
not feel like making 2 different cases when I don't know where they
should be placed.
As I said in my first mail, I think it should use { target c++20 } and
then use chunk_view conditionally. The libstdc++ devs routinely test
with GLIBCXX_TESTSUITE_STDS=... defined so that it will still be
tested in C++23 mode even if the { targt c++20 } doesn't force that
for all testers.
I specifically made a dummy iterator to disable the views iterator's
operator<=>. At a glance, it seems like the iterators in
testsuite_iterators.h might be suitable for that purpose. However, I
wasn't sure and wanted a testcase I can be sure tests what I needed it
to test for now. If preferred, I'll fix the testcase to use
testsuite_iterators.h instead.
The testsuite's random_access_iterator_wrapper doesn't *currently*
define operator<=> but it's possible that could change at some point,
so it makes sense to define your own that guarantees the property that
the test relies on.
(I suppose we could add operator<=> to the testsuite one and guard it
with a macro like SPACESHIP_FOR_ITERATOR_WRAPPER so individual tests
can choose whether to enable it or not ... but that doesn't need to be
part of your fix.)
Alex
From 3af8b200b917f7df4936d36dd10cc3c3dc1ba308 Mon Sep 17 00:00:00 2001
From: Waffl3x <[email protected]>
Date: Thu, 3 Dec 2026 19:13:12 -0600
Subject: [PATCH] libstdc++: Fix comparison operators in elements and chunk
views
A few comparison operators mistakenly used the wrong operator in their
implementation. This likely fell through the cracks as non <=> operators
are only ever used if the underlying iterator is not three_way_comparable.
libstdc++-v3/ChangeLog:
* include/std/ranges
(elements_view::_Iterator::operator<=): Use < (less than) instead
instead of > (greater than) in function body.
(elements_view::_Iterator::operator>=): Likewise.
(chunk_view<_Vp>::_Iterator::operator<): Likewise.
* testsuite/std/ranges/adaptors/iterator_compare.cc: New test.
---
libstdc++-v3/include/std/ranges | 6 +-
.../std/ranges/adaptors/iterator_compare.cc | 162 ++++++++++++++++++
2 files changed, 165 insertions(+), 3 deletions(-)
create mode 100644
libstdc++-v3/testsuite/std/ranges/adaptors/iterator_compare.cc
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 5963647c378..95fee4ede20 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -4642,12 +4642,12 @@ namespace views::__adaptor
friend constexpr bool
operator<=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
- { return !(__y._M_current > __x._M_current); }
+ { return !(__y._M_current < __x._M_current); }
friend constexpr bool
operator>=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
- { return !(__x._M_current > __y._M_current); }
+ { return !(__x._M_current < __y._M_current); }
#ifdef __cpp_lib_three_way_comparison
friend constexpr auto
@@ -6727,7 +6727,7 @@ namespace views::__adaptor
friend constexpr bool
operator<(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
- { return __x._M_current > __y._M_current; }
+ { return __x._M_current < __y._M_current; }
friend constexpr bool
operator>(const _Iterator& __x, const _Iterator& __y)
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/iterator_compare.cc
b/libstdc++-v3/testsuite/std/ranges/adaptors/iterator_compare.cc
new file mode 100644
index 00000000000..d8ff20dcb99
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/iterator_compare.cc
@@ -0,0 +1,162 @@
+// { dg-do run { target c++23 } }
This should be c++20.
+// { dg-add-options no_pch }
Remove this dg-add-options.
+
+#include <ranges>
+
+#include <testsuite_hooks.h>
+
+template<typename ValueType>
+struct no_three_way_iter
+{
+ int _v;
+
+ using difference_type = long long;
+ using value_type = ValueType;
+ using iterator_category = std::random_access_iterator_tag;
If you remove the iterator_category member then this isn't trying to
pretend to be a Cpp17RandomAccessIterator and it's fine for it to
return a prvalue instead of a reference.
+ using iterator_concept = std::random_access_iterator_tag;
+
+ constexpr value_type
+ operator*() const
+ { return {}; }
+
+ constexpr value_type
+ operator[](difference_type) const
+ { return {}; }
+
+ constexpr no_three_way_iter&
+ operator++()
+ {
+ ++_v;
+ return *this;
+ }
+
+ constexpr no_three_way_iter
+ operator++(int)
+ {
+ no_three_way_iter tmp{*this};
+ ++(*this);
+ return tmp;
+ }
+
+ constexpr no_three_way_iter&
+ operator--()
+ {
+ --_v;
+ return *this;
+ }
+
+ constexpr no_three_way_iter
+ operator--(int)
+ {
+ no_three_way_iter tmp{*this};
+ --(*this);
+ return tmp;
+ }
+
+ constexpr no_three_way_iter&
+ operator+=(difference_type n)
+ {
+ _v += static_cast<int>(n);
+ return *this;
+ }
+
+ constexpr no_three_way_iter&
+ operator-=(difference_type n)
+ {
+ _v -= static_cast<int>(n);
+ return *this;
+ }
+
+ friend constexpr no_three_way_iter
+ operator+(no_three_way_iter const& it, difference_type n)
+ {
+ no_three_way_iter r = it;
+ r += n;
+ return r;
+ }
+
+ friend constexpr no_three_way_iter
+ operator+(difference_type n, no_three_way_iter const& it)
+ {
+ auto r = it;
+ r += n;
+ return r;
+ }
+
+ friend constexpr no_three_way_iter
+ operator-(no_three_way_iter const& it, difference_type n)
+ {
+ auto r = it;
+ r -= n;
+ return r;
+ }
+
+ friend constexpr difference_type
+ operator-(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ {
+ return lhs._v - rhs._v;
+ }
+
+ friend constexpr bool
+ operator==(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ { return lhs._v == rhs._v; }
+
+ friend constexpr bool
+ operator<(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ { return lhs._v < rhs._v; }
+
+ friend constexpr bool
+ operator>(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ { return lhs._v > rhs._v; }
+
+ friend constexpr bool
+ operator<=(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ { return lhs._v <= rhs._v; }
+
+ friend constexpr bool
+ operator>=(no_three_way_iter const& lhs, no_three_way_iter const& rhs)
+ { return lhs._v >= rhs._v; }
+
+ constexpr friend auto operator<=>(no_three_way_iter const&, no_three_way_iter
const&) = delete;
+};
I'd like to have:
static_assert( std::random_access_iterator<no_three_way_iter<int>> );
just so we know that it provides everything it should provide, and if
the test starts to fail we know it's something else.
+
+template<typename ValueType>
+struct no_three_way_iter_range
+{
+ int _begin;
+ int _end;
+
+ constexpr no_three_way_iter<ValueType>
+ begin() const
+ { return {_begin}; }
+
+ constexpr no_three_way_iter<ValueType>
+ end() const
+ { return {_end}; }
+};
+
+template<typename View>
+void test_view_iter_comparisons(View view)
+{
+ auto it1 = std::ranges::begin(view);
+ auto it2 = std::ranges::next(it1);
+
+ VERIFY((it1 < it2) == (it1.base() < it2.base()));
+ VERIFY((it2 < it1) == (it2.base() < it1.base()));
+ VERIFY((it1 <= it2) == (it1.base() <= it2.base()));
+ VERIFY((it2 <= it1) == (it2.base() <= it1.base()));
+ VERIFY((it1 > it2) == (it1.base() > it2.base()));
+ VERIFY((it2 > it1) == (it2.base() > it1.base()));
+ VERIFY((it1 >= it2) == (it1.base() >= it2.base()));
+ VERIFY((it2 >= it1) == (it2.base() >= it1.base()));
+ VERIFY((it1 <= it1) == (it1.base() <= it1.base()));
+ VERIFY((it1 >= it1) == (it1.base() >= it1.base()));
+}
+
+int main()
+{
+ no_three_way_iter_range<int> r = {1, 10};
+ test_view_iter_comparisons(r | std::views::chunk(3));
Guard the two lines above with:
#ifdef __cpp_lib_ranges_chunk // C++ >= 23
Assuming the test still passes with those changes, this is OK for
trunk and all release branches (the bug has been present since gcc-10
for elements_view when that view was added, and since gcc-13 when
chunk_view was added). Thanks!
+ no_three_way_iter_range<std::pair<int, int>> r2 = {1, 10};
+ test_view_iter_comparisons(r2 | std::views::elements<0>);
+}
--
2.55.0