http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46906
--- Comment #6 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2011-09-05 11:11:26 UTC --- (In reply to comment #5) > On the other hand, it looks like I can > construct i2 from s (instead of copying from i1) and still hit the same issue > with a valid program. Do you agree? (Hmm, could the standard make it undefined > to interlace uses of an istreambuf_iterator and other operations on the > istreambuf, to allow this behavior?) You still hit the same result, but the result has nothing to do with some special implementation details of std::istreambuf_iterator: 1) The fact that repeated calls of operator* without intervening operator++ calls produce the same result for a given iterator object is required by expression *a: "The expression (void)*a, *a is equivalent to *a." This explains the repeated values '1' and '1' from it1 and '2' and '2' from it2. 2) The observation that the last output produces a "3" for the first iterator is to be expected from the fact, that std::istreambuf_iterator is a shallow wrapper for the actual stream buffer, as described in [istreambuf.iterator] p1 says: "The class template istreambuf_iterator defines an input iterator (24.2.3) that reads successive characters from the streambuf for which it was constructed." Any "external" accesses to that stream buffer (and the usage of an effective sbumpc() call via the second std::istreambuf_iterator object on the same stream buffer) obviously changes the state of the stream buffer in the expected way. The same result would be observed when you would replace the second iterator object by explicit calls of the stream buffer as follows: #include <ios> #include <istream> #include <sstream> #include <iostream> using namespace std; int main(){ istringstream s("1234"); istreambuf_iterator<char> i1(s); std::basic_streambuf<char>& b = *s.rdbuf(); std::cerr << *i1 << (char) b.sgetc() << '\n'; b.sbumpc(); char c = b.sgetc(); std::cerr << *i1 << c << '\n'; ++i1; std::cerr << *i1 << c << '\n'; } I'm getting: 11 12 32 which is non-distinguishable from the example program with two different std::istreambuf_iterator objects. I don't see why there would be undefined behaviour involved.