https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122224

            Bug ID: 122224
           Summary: std::prev when called with non-bidirectional iterators
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: terra at gnome dot org
  Target Milestone: ---

Created attachment 62525
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62525&action=edit
Preprocessed source

Calling std::prev on a iterator that isn't bidirectional is wrong.
Should this compile?  (See also bug 122194.)

Bug 62039 is similar, but claimed fixed ages ago.

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.  



$ cat ttt.C 
#include <iostream>
#include <sstream>
#include <iterator>

int
main(int, char **)
{
  std::istringstream str { "Howdy!" };

  auto it = std::istream_iterator<char>(str);
  auto it2 = std::next(it);
  auto it3 = std::prev(it2);

  return 0;
}


$ g++ -std=gnu++23  ttt.C 
$ ./a.out 
/usr/local/products/gcc/15.1.0/include/c++/15.1.0/bits/stl_iterator_base_funcs.h:163:
constexpr void std::__advance(_InputIterator&, _Distance, input_iterator_tag)
[with _InputIterator = istream_iterator<char>; _Distance = long int]: Assertion
'__n >= 0' failed.
Aborted (core dumped)
$ g++ -std=gnu++23  -O ttt.C 
$ ./a.out 
^C

$ g++ -v
Using built-in specs.
COLLECT_GCC=/usr/local/products/gcc/15.1.0/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/products/gcc/15.1.0/lib/gcc/x86_64-suse-linux/15.1.0/lto-wrapper
Target: x86_64-suse-linux
Configured with: ../../gcc-15.1.0/configure
--enable-languages=c,c++,fortran,jit --enable-host-shared --with-pic
--enable-targets=x86_64-suse-linux,i686-suse-linux
--prefix=/usr/local/products/gcc/15.1.0 --with-gnu-as
--with-as=/usr/local/products/gcc/binutils-2.44/bin/as
--with-ld=/usr/local/products/gcc/binutils-2.44/bin/ld.gold --enable-link-mutex
--enable-gnu-indirect-functions --enable-linux-futex --enable-threads=posix
--enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new
x86_64-suse-linux --enable-libstdcxx-backtrace=yes --with-zstd
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.1.0 (GCC)

Reply via email to