[PATCH] libstdc++: Fix comparison operators in elements and chunk views

Patrick Palka ppalka@redhat.com
Fri Dec 4 14:52:30 GMT 2026


On Fri, 4 Dec 2026, Jonathan Wakely wrote:

> On Fri, 4 Dec 2026 at 13:02, Patrick Palka <ppalka@redhat.com> wrote:
> >
> > On Fri, 4 Dec 2026, 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.
> > >
> > > 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.
> > >
> > > Alex
> > >
> >
> > > 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); }
> >
> > I'd prefer swapping x and y, not the operator:
> >
> >   { return !(__x._M_current > __y._M_current); }
> 
> Doesn't the standard specify all of these operators only in terms of < though?

D'oh, yeah, and that's how we've been consistently implementing it.
Never mind.

> 
> 
> >
> > >
> > >         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 } }
> > > +// { dg-add-options no_pch }
> > > +
> > > +#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;
> > > +  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;
> > > +};
> > > +
> > > +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));
> > > +  no_three_way_iter_range<std::pair<int, int>> r2 = {1, 10};
> > > +  test_view_iter_comparisons(r2 | std::views::elements<0>);
> > > +}
> > > --
> > > 2.55.0
> > >
> >
> 
> 



More information about the Libstdc++ mailing list