Why do C++ streams use char instead of unsigned char?

后端 未结 4 427
鱼传尺愫
鱼传尺愫 2020-12-02 14:13

I\'ve always wondered why the C++ Standard library has instantiated basic_[io]stream and all its variants using the char type instead of the unsigned char

4条回答
  •  情歌与酒
    2020-12-02 14:54

    Possibly I've misunderstood the question, but conversion from unsigned char to char isn't unspecified, it's implementation-dependent (4.7-3 in the C++ standard).

    The type of a 1-byte character in C++ is "char", not "unsigned char". This gives implementations a bit more freedom to do the best thing on the platform (for example, the standards body may have believed that there exist CPUs where signed byte arithmetic is faster than unsigned byte arithmetic, although that's speculation on my part). Also for compatibility with C. The result of removing this kind of existential uncertainty from C++ is C# ;-)

    Given that the "char" type exists, I think it makes sense for the usual streams to use it even though its signedness isn't defined. So maybe your question is answered by the answer to, "why didn't C++ just define char to be unsigned?"

提交回复
热议问题