https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97828
--- Comment #2 from ensadc at mailnesia dot com --- This seems to fix the bug (note: not thoroughly tested) diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h index f1a4cc24c0d..414ce0b1baa 100644 --- a/libstdc++-v3/include/bits/ranges_algo.h +++ b/libstdc++-v3/include/bits/ranges_algo.h @@ -579,21 +579,24 @@ namespace ranges } } - if constexpr (sized_sentinel_for<_Sent, _Iter>) + if constexpr (random_access_iterator<_Iter> + && sized_sentinel_for<_Sent, _Iter>) { auto __tail_size = __last - __first; auto __remainder = __count; while (__remainder <= __tail_size) { + auto __t = __remainder; __first += __remainder; __tail_size -= __remainder; auto __backtrack = __first; while (__value_comp(std::__invoke(__proj, *--__backtrack))) { - if (--__remainder == 0) + if (--__t == 0) return {__first - __count, __first}; } + __remainder = __count - __remainder + __t; } auto __i = __first + __tail_size; return {__i, __i};