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: Strange stream behaviour


On Sun, Oct 07, 2001 at 02:12:54PM +0100, Jason Merrill wrote:
> >>>>> "Nathan" == Nathan Myers <ncm@nospam.cantrip.org> writes:
> 
> > There is no reason to expect the program to work.  In particular, the 
> > function cin.get() can never return EOF under any circumstances.  EOF 
> > is an int constant, where cin.get() can only return character values.
> 
> Gosh, that seems like a big ol' trap for new users.  Is EOF checking a
> casualty of templatization?

No.  In-band EOF checking was a fundamental design mistake in stdio.
It was fixed in istream, but unfortunately not in streambuf.  The
correct way to provide an EOF check is to provide a predicate that
tests the stream itself, independent of the stream content.  In the
nominated example, the correct (albeit very, very slow) code looks 
like

  while (cin) cout.put(cin.get());

Such a loop would be more or less equivalent, in stdio-land, to

  char c; while (scanf("%c", &c) == 1) printf("%c", c);

which of course no one would write.

The trap for new users is set by irresponsible writers about C++ 
who fail to explain streambufs, and lead users to do low-level 
I/O with the equivalents of printf and scanf.

Nathan Myers
ncm at cantrip dot org


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