Question about std::basic_ostream and unsigned char in gcc 3.0

Stephen M. Webb
Thu Mar 22 06:24:00 GMT 2001

> The unfortunate thing is, this piece of code passes unsigned
> char to instances std::ostream in lots of places.  What is
> the safest way to fix the code?  Right now, I've been going
> and adding a lot of casts like:
>     ostr.write( (char *)buf, 18);
> Is this cast safe, or will I be losing something?
> SAMPEG was written to write binary MPEG data files, so I think
> that was the intent of using unsigned char in ostream.

I would think your best bet would be to change all the unsigned chars to
plain chars.  You should use chars unless (a) you're doing arithmetic, in
which case you need signed chars, or (2) you're explicitly performing
unsigned ordering comparisons on unsigned char values.  If you're just
moving bytes around, use plain chars.

Stephen M. Webb

More information about the Libstdc++ mailing list