This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/51881] istream_iterator copy leads to incorrect value read


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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-17 13:07:04 UTC ---
(In reply to comment #0)
> 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.

You need to fix your operator>> so that it gives the value {4,5,6} to the foo
object, the bug is that you don't do that

This has nothing to do with istream_iterator, consider:

int main(int argc, char *argv[])
{
    std::stringstream ss;

    ss << "1 2 3\n4 5 6";

    foo f;
    f.v = std::vector<int>(3);
    ss >> f;
    std::cout << f << '\n';

    return 0;
}

This shows that your operator>> is buggy, it doesn't read a value from the
stream and set that value in the foo argument, it reads a value and appends it
to the foo argument.

You should reset the foo argument in your operator>>, or read into a temporary
then assign that to (or swap it with) the foo argument.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]