This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


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

Re: istream && istream_iterator behavior


I swear I sent this mail already...  okay maybe I'm insane.


On Fri, Feb 23, 2001 at 12:58:25PM +0100, Laurent Marzullo wrote:
> $ g++ essai.cpp
> $ ./a.out
> 1234
> read1 = `1'
> read2 = `2'	// ?????? I was expected `1' too
> 
> May someone explain me why I've got this output and not
> read2 = `1' ??
> 
> Is this a correct behavior ?

I believe this is correct.  Here's what's happening:

When an istream_iterator<char>(s) is constructed, one character is read
and cached from s.  This means that s moves forward to the next character.

You have read1's l_in_ptr being constructed, which reads and caches '1'.
Then read2's l_in_ptr is constructed, which reads and caches '2'.  If read1
were to use

    cout << "read1 = `" << (*l_in_ptr) << "'\n";
    cout << "read1 = `" << (*l_in_ptr) << "'\n";

twice before calling read2(), then I would expect '1' to be printed both
times, because the cached character is being printed both times.

The Standard /allows/ the cached character to be read at construction time,
but does not /require/ it, so do not rely on this behavior.


Luck++;
Phil

-- 
pedwards at disaster dot jaj dot com  |  pme at sources dot redhat dot com
devphil at several other less interesting addresses in various dot domains
The gods do not protect fools.  Fools are protected by more capable fools.


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