Strange stream behaviour

Nathan Myers ncm@nospam.cantrip.org
Sun Oct 7 13:06:00 GMT 2001


On Sun, Oct 07, 2001 at 04:30:17PM +0200, Gabriel Dos Reis wrote:
> Jason Merrill <jason_merrill@redhat.com> writes:
> 
> | >>>>> "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?
> 
> I think the canonical way to check for end-of-file uses 
> 
> 	typedef std::char_traits<character-type> char_traits;
> 	if (char_traits::eq_int_type(characrter, char_traits::eof()))
>           ...
> which operates on the "integer"-type representation of characters.

The above is correct in template code parameterized on the character type.  

When you know you have an ordinary "char" stream, comparing a streambuf 
result with vanilla EOF remains correct, and Gaby's code above reduces 
to the same instruction sequence.

Nathan Myers
ncm at cantrip dot org



More information about the Libstdc++ mailing list