https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125784
Bug ID: 125784
Summary: Improve ranges::advance?
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: hewillk at gmail dot com
Target Milestone: ---
This is not a bug in libstdc++, but rather an area that can be improved in
ranges::advance.
For the following example:
views::iota(0) | std::views::drop(n);
the standard specifies that the return begin iterator of drop_view is
ranges::next(ranges::begin(r), n, ranges::end(r)). However, the time complexity
is always O(n) even with random-access iterators.
I'm wondering if it's reasonable to make a new branch for ranges::advance, for
example:
else if constexpr (same_as<_Sent, unreachable_sentinel_t>)
(*this)(__it, __n);
This at least makes it O(1) in the case of random-access.
I know that this example is UB by current standards because the result of the
application of library functions to invalid ranges (which is infinite range in
this case) is UB, but these examples are not nonsense in real-world.
Feel free to close it if the library author deems it not worthwhile.