Topic: I/O and unsigned char
Author: =?UTF-8?B?S8WZacWhdG9mIMW9ZWxlY2hvdmpza2k=?=<giecrilj@stegny.2a.pl>
Date: Tue, 20 Dec 2011 09:17:29 -0800 (PST)
Raw View
* Why is ostream ::operator<< (unsigned char) not defined?
I do not know, but see below.
* Why is operator<< (ostream, unsigned char) defined?
Because otherwise operator>> (istream, unsigned char&) would do the
wrong thing:
unsigned char const c ((25)); unsigned char d;
ostream<< c; istream>> d; // Oops, d == '2'
* Why does operator>> (istream, unsigned char&) do the wrong thing?
Because at the time when it was defined, the C library did not provide
any method for reading a variable of type unsigned char. This problem
was fixed in C99. (Were it supported, this operator would have been
defined as a member function.)
* So why is operator>> (istream, unsigned char&) defined?
I suppose that BS preferred having broken I/O to having no I/O in this case.
* Why is operator>> (istream, unsigned char []) defined?
It is a logical consequence of having istream ::operator>> (unsigned
char&) do the wrong thing. This one is particularly illogical, since
neither ctype< unsigned char> nor char_traits< unsigned char> need
to exist.
* So why is istream ::read (unsigned [], streamsize) not defined?
This one, in contrast to the previous ones, would be useful for binary
I/O. It may stem from the fact that at the time void did not exist, the
idiom for a raw pointer was char *, not unsigned char *, which would be
more appropriate, maybe because unsigned char is longer to type, and
typedef pointer was considered too trivial to define.
<URL: http://adrinael.net/wrong6>
Chris
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]