public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "daniel.kruegler at googlemail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/46906] istreambuf_iterator is late?
Date: Mon, 05 Sep 2011 11:11:00 -0000	[thread overview]
Message-ID: <bug-46906-4-RM34Po90EO@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-46906-4@http.gcc.gnu.org/bugzilla/>

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.


  parent reply	other threads:[~2011-09-05 11:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-12 15:26 [Bug libstdc++/46906] New: " marc.glisse at normalesup dot org
2010-12-12 17:38 ` [Bug libstdc++/46906] " paolo.carlini at oracle dot com
2010-12-12 18:01 ` marc.glisse at normalesup dot org
2010-12-12 18:28 ` paolo.carlini at oracle dot com
2011-09-05  9:49 ` daniel.kruegler at googlemail dot com
2011-09-05 10:00 ` marc.glisse at normalesup dot org
2011-09-05 11:11 ` daniel.kruegler at googlemail dot com [this message]
2011-09-05 12:39 ` marc.glisse at normalesup dot org
2011-09-05 12:57 ` daniel.kruegler at googlemail dot com
2011-09-05 14:02 ` marc.glisse at normalesup dot org
2011-09-05 14:17 ` daniel.kruegler at googlemail dot com
2011-09-05 15:06 ` marc.glisse at normalesup dot org
2012-08-22 20:00 ` glisse at gcc dot gnu.org
2023-05-17 14:46 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-46906-4-RM34Po90EO@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).