Why C and C++ hate signed char so much?

前端 未结 3 1435
旧巷少年郎
旧巷少年郎 2021-01-11 10:47

Why does C allow accessing object using "character type":

6.5 Expressions (C)

An object shall have its stored value acc

3条回答
  •  情歌与酒
    2021-01-11 11:39

    I think what you're really asking is why signed char is disqualified from all the rules allowing type-punning to char* as a special case. To be honest, I don't know, especially since — as far as I can tell — signed char cannot have padding either:

    [C++11: 3.9.1/1]: [..] A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. [..]

    Empirical evidence suggests that it's not much more than convention:

    • char is seen as a byte of ASCII;
    • unsigned char is seen as a byte with arbitrary "binary" content; and
    • signed char is left flapping in the wind.

    To me, it doesn't seem like enough of a reason to exclude it from these standard rules, but I honestly can't find any evidence to the contrary. I'm going to put it down to a mildly inexplicable oddity in the standard wording.

    (It may be that we have to ask the std-discussion list about this.)

提交回复
热议问题