c++ckeyword

Can the unsigned keyword be used in non-obvious ways?


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 used.

If not, why does it have its own keyword? Why is unsigned int not uint?


Solution

  • 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.