https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97828
Bug ID: 97828
Summary: std::ranges::search_n does not work with
counted_iterator<_List_iterator<...>>
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Keywords: rejects-valid
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: ensadc at mailnesia dot com
Target Milestone: ---
```
#include <ranges>
#include <algorithm>
#include <iostream>
#include <list>
int main() {
using namespace std::ranges;
std::list a = {0,42,42,0,42,42,42,0};
auto b = a | views::take(5);
auto count = 3;
auto res = search_n(b, count, 42);
for (int v : res) std::cout << v << ' ';
std::cout << '\n';
}
```
This gives a rather long error message.
It appears that the `sized_sentinel_for` branch of std::ranges::search_n uses
random access iterator operations, without checking if the iterator is actually
random access.
(This branch also returns incorrect results for certain input, which can be
observed by changing `std::list` to `std::vector`.)