I had totally forgotten about it myself. The occasion to take a fresh look at it. Here is a new version considering 1st pubseekoff call returned value to find out if it can be used. I removed the check/comparison with 2nd call result as it's not usable. In new 4_neg.cc test case if I ask to advance istreambuf_iterator by 100000 the returned pos_type is showing 100000 offset even if it is obviously false. You need to read next byte to see that it is eof.     libstdc++: Implement std::advance for istreambuf_iterator using pubseekoff     If advance increment is smaller than input buffer size just advance in this     buffer thanks to __safe_gbump. If increment is larger check for seekoff support     and use it accordingly. Otherwise fallback on current __safe_gbump/underflow loop.     libstdc++-v3/ChangeLog:             * include/bits/streambuf_iterator.h (advance): Re-implement             using streambuf pubseekoff/seekoff when supported.             * testsuite/25_algorithms/advance/istreambuf_iterators/char/4_neg.cc: New             test case. Ok to commit when back in stage 1 ? François On 31/03/2023 23:03, Jonathan Wakely wrote: > On Tue, 15 Oct 2019 at 21:20, François Dumont wrote: >> Here is an update to set _M_sbuf to null if the user advance too much. >> >> Note that in this case the streambuf remains un-modified which is >> different from the current implementation. I think it is another >> enhancement. >> >> I also change the Debug assertion message for something more dedicated >> to std::advance algo. >> >> François >> >> On 10/14/19 10:12 PM, François Dumont wrote: >>> The same way I proposed to review std::copy overload for >>> istreambuf_iterator we can implement std::advance using pubseekoff. >>> >>> It is both a cleaner implementation and avoids yet another friend >>> declaration. > Looks like I never sent my review of this one, it's been sitting in my > draft mails for years, sorry. > > It looks like this will fail if the streambuf doesn't support seeking. > The default behaviour for seekoff is to return -1, in which case > you'll get -1 for both calls to pubseekoff, and new_pos - cur_pos will > be zero, which is not equal to n, so you set the istreambuf_iterator > to end-of-stream. That seems wrong, we could still advance using the > old code (or just call ++ in a loop!) > > > >>> * include/std/streambuf >>> (advance(istreambuf_iterator<>&, _Distance)): Remove friend >>> declaration. >>> * include/bits/streambuf_iterator.h (__copy_move_a2): Re-implement >>> using >>> streambuf pubseekoff. >>> >>> Tested under Linux x86_64. >>> >>> Ok to commit ? >>> >>> François >>>