Following the question titled Warning generated due wrong strcmp parameter handling, there seems to be some questions regarding what the Standard actually guarantees regardi
but there's no such guarantee in the C++11 Standard (N3337), nor in the upcoming C++14 (N3797).
char unsigned * p1 = ...;
char * p2 = reinterpret_cast (p1);
*p1 == *p2; // (1), not guaranteed to be true
Note: it is implementation specific whether char is signed or unsigned; [basic.fundamental]p1.
The Standard guarantees that every character type shall;
Sharing the same amount of storage, alignment requirement, and the guarantee about bit participation, means that casting a lvalue referring to one type (unsigned char), to another (char), is safe.. as far as the actual cast is concerned.
3.9.1p1Fundamental types[basic.fundamental]It is implementation-defined whether a
charcan hold negative values. Characters can be explicitly declaredsignedorunsigned.A
char,asigned char,and anunsigned charoccupy 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.For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.
3.9p4Types[basic.types]The object representation of an object of type
Tis the sequence of Nunsigned charobjects taken up by the object of typeT,whereNequalssizeof(T). The value representation of an object is the set of bits that hold the value of typeT.
If we assign the maximum value of an unsigned char (UCHAR_MAX) to *p1 and *p2 is signed, *p2 won't be able to represent this value. We will overflow *p2 and it will, most likely, end up having the value of -1.
Note: signed integer overflow is actually undefined behavior.
*p1 = UCHAR_MAX;
*p1 == *p2; // (1)
Both sides of operator== must have the same type before we can compare them, and currently one side is unsigned char and the other char.
The compiler will therefor resort to integral promotion to find a type that can represent all combined possible values of the two types; and in this case the resulting type will be int.
After the integral promotion the statement is semantically equivalent to int (UCHAR_MAX) == int(-1), which of course is false.