Strictly speaking, it depends on how many bits there are in a char
for the target in question. You're right that for a normal 2's complement machine with a signed 8-bit char
type, that they should probably be using unsigned char
.
The conversion from signed to unsigned, should one take place, is well-defined according to the C++ spec (4.7 Integral conversions):
2 If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —endnote]
The conversion from unsigned to signed, however, is implementation-defined:
3 If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.
But in practice, most systems work they way you'd expect and things go fine. That is, signed overflow works like unsigned overflow, the bit patterns don't change regardless of signed->unsigned or unsigned->signed conversions, etc.