We all know and often use the unsigned keyword, but have you ever thought of the corresponding signed keyword?
Copy Code code as follows:
int i = 0;
signed int i = 0;
Is there a difference between these two? No difference, it seems, signed is a total drag.
Is that really the case?
I consulted the C++11 standard document (draft N3690) and found some clues:
3.9.1 Fundamental Types
Copy Code code as follows:
Objects declared as characters (char) shall is large enough to store any member of the implementation ' s basic character se T. If a character from this set are stored in a character object, the integral value of that character object are equal to T He value of the "a" character literal form of that character. It is implementation-defined whether a Char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain Char, signed char, and unsigned char are three distinct types, collectively called, narrow character. A Char,a signed Char,and an unsigned char occupy the same amount of storage and have the same alignment (3.11) ; That Is,they have the same object representation. For narrow character types, all bits of the object representation participate in the value representation. For unsigned narrow character types, all possible bit patterns of the value representation represent. These requirements don't hold for the other types. In aY particular implementation, a plain char object can take on either the same values as a signed char or a unsigned char; Which one is implementation-defined.
The standard rules are clear, Char, signed char and unsigned char are three different types. Char will determine whether it is signed or unsigned, depending on the actual implementation of the scenario.
What about the C11 standard document (ISO/IEC 9899:201x)?
6.7.2 Type Specifiers
Copy Code code as follows:
Each of the comma-separated multisets designates the same type, except this for bit-fields, it is implementation-defined whe ther the specifier int designates the same type as signed int or the same type as unsigned int.
It seems that bit-fields (bit field) also have the same problem. (Bit-field concept may also be a bit biased, often write comparison of the underlying interface or protocol child shoes should be familiar, can refer to here)
Conclusion
In C + +, the signed keyword is cumbersome in most cases, but for the above mentioned two situations, that is, in the use of char and bit-fields, there is a relatively vague role.
It's always good to give yourself a wake-up call.