[Bug libstdc++/122224] std::prev when called with non-bidirectional iterators
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Oct 9 20:08:33 GMT 2025
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122224
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to M Welinder from comment #0)
> Created attachment 62525 [details]
> Preprocessed source
>
> Calling std::prev on a iterator that isn't bidirectional is wrong.
That's unclear: https://cplusplus.github.io/LWG/issue3197
> Should this compile? (See also bug 122194.)
I think so, yes. std::prev(it, n) is equivalent to std::advance(it, -n) and so
if you do std::prev(it, -1) then it's the same as ++it which doesn't need a
bidirectional iterator.
So we accept calling std::prev(it, n) on input iterators, but assert if n >= 0.
> For me, it compiles. Without -O, it asserts. With -O, I get an infinite
> loop. The former is better than the latter but a compile error would be
> better.
Without -O you get -D_GLIBCXX_ASSERTIONS by default, and there's an assertion
that
More information about the Gcc-bugs
mailing list