Every time I've used the unsigned
keyword it has been before int
, or another built-in type. I was wondering if there were any other ways unsigned
could be 开发者_JAVA百科used.
- Can user defined types (classes/structs) use the
unsigned
keyword? - Can templates make special use with
unsigned
?
If not, why does it have its own keyword? Why is unsigned int
not uint
?
The main question has been answered several times: the unsigned
keyword can only be used as a type-specifier for an integral type.
As for why unsigned
is a separate keyword, rather than having, say, a uint
keyword, the reasons for that are historical.
The earliest versions of C (pre-K&R) had only four fundamental types:
char
(8 bits, signed, 2's-complement)int
(16 bits, signed, 2's-complement)float
(32 bits)double
(64 bits, same range asfloat
but greater precision)
Note what's missing: no signed
or unsigned
keywords, no short
, long
, or long double
; all those were added later. (Programmers who needed unsigned arithmetic commonly used pointers, which were freely interchangeable with int
.)
Each fundamental type had a name that was a single keyword, which made the grammar straightforward.
When other types were added later, it made sense to add specifiers like unsigned
, short
, and long
to the existing type names rather than introducing new keywords (which might have broken existing code). When the ANSI C committee standardized the language in 1989, they had to make a coherent structure out of the existing not-quite-formal definitions while remaining consistent with existing implementations. The result is what we have now, where int long unsigned long
is a valid type name (more commonly written as unsigned long long
).
If the language were being designed from scratch now, I suspect that a different approach would have been taken. Perhaps there would be a single keyword for each fundamental type (that's the approach taken by C#, for example), or perhaps the fundamental type names would use some more coherent scheme rather than a jumble of keywords (say, int:2
for a 2-byte integer, unsigned:4
for a 4-byte unsigned integer). But both C and C++ are stuck with the current approach.
Reference: http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf
No, it can't be used with classes or structs since they're not integral types. All a template can do with it is make an int unsigned. I think it was chosen as a separate keyword since it can be applied to any integer type (char, int, long, long long), thereby achieving with one keyword what would have required four more. Its meaning is also immediately evident, whereas uint, uchar, etc. aren't necessarily. And keep in mind it can also be used by itself without a qualifier, in which case unsigned int is assumed.
unsigned
is a keyword. unsigned
can only be used on integral types. unsigned
or signed
is considered a type-specifier, a simple type-specifier. So they specify that the type will either by signed or unsigned.
You can typedef
the words unsigned int
to uint
but then that would make it look like a type when int
is the type and unsigned
is the type-specifier per se.
You can use unsigned
by itself, contrary to popular belief, as evidenced in the C++ ISO Standard Section 7.1.5.2 Table 7:
You can use multiple type-specifiers (when only allowed) and can be freely mixed with decl-specifiers in any order.
You can also do this:
int unsigned i;
and this is valid C++.
No unsigned isn't really a keyword on its own; it's only a modifier for int, short, char.
The unsigned
keyword can be used as a modifier for int
, short
, char
, or long
. It can also be used on its own as a type name; unsigned
means unsigned int
.
It was done that way to avoid having three extra keywords uint ushort uchar and because signed/unsigned might not have been different on all the early computers C and Unix were aimed at.
The keywords signed, unsigned, short, and long are type modifiers/specifier and When one of these type modifiers/specifier is used by itself, a data type of int is assumed.
So signed and unsigned may also be used as standalone type specifiers
, meaning the same as signed int and unsigned int respectively. The following two declarations are equivalent:
unsigned abc;
unsigned int abc;
There is also unsigned char. Some compilers, such as GNU's g++, let you just put unsigned and assume you mean unsigned int. For example the following code is equivalent
unsigned int x;
unsigned x;
精彩评论