http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51881

             Bug #: 51881
           Summary: istream_iterator copy leads to incorrect value read
    Classification: Unclassified
           Product: gcc
           Version: 4.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: roncar...@lavabit.com


Created attachment 26350
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26350
program that shows incorrect behaviour

=================================================================
gcc version 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) 

=================================================================
Linux localhost.localdomain 2.6.32-71.29.1.el6.x86_64 #1 SMP Mon Jun 27
19:49:27 BST 2011 x86_64 x86_64 x86_64 GNU/Linux


=================================================================
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix

=================================================================
g++ main.cc -g --save-temps -Wall

=================================================================
expected behaviour:
we have a collection of classes: each class contains a vector of values (i.e.
int). We want to read the class from a stream that contains a class for each
line.
i.e: reading a stream that contains this:
1 2 3\n4 5 6
would get an array with two instances of the class: 
one with the array {1 2 3}
the other with the array {4 5 6}
using istream_iterator<T> in a for loop with prefix increment works correctly

=================================================================
actual behavior:
using istream_iterator<T> in a for loop with postfix increment (or with
std::copy and a back_inserter) does not work correctly:
the first instance of the class has values {1 2 3}
the second {1 2 3 4 5 6}

>From my analysis this seams to stem from a copy of istream_iterator<T> 
that contains an instance of T inside std::copy

Since stream_iterator.h _M_read() works on this copy (_M_value) the second
application of operator >> () simply append 4 5 6 to the values already
present.

Reply via email to