How to write an unsigned short int literal?

后端 未结 9 1396
慢半拍i
慢半拍i 2021-01-07 15:53

42 as unsigned int is well defined as \"42U\".

unsigned int foo = 42U; // yeah!

How can I write \"23\" so that it is clear it is an

相关标签:
9条回答
  • 2021-01-07 16:53

    There are no modifiers for unsigned short. Integers, which has int type by default, usually implicitly converted to target type with no problems. But if you really want to explicitly indicate type, you could write the following:

    unsigned short bar = static_cast<unsigned short>(23);
    

    As I can see the only reason is to use such indication for proper deducing template type:

    func( static_cast<unsigned short>(23) );
    

    But for such case more clear would be call like the following:

    func<unsigned short>( 23 );
    
    0 讨论(0)
  • 2021-01-07 16:56

    Unfortunately, they can't. But if people just look two words behind the number, they should clearly see it is a short... It's not THAT ambiguous. But it would be nice.

    0 讨论(0)
  • 2021-01-07 16:57

    You probably shouldn't use short, unless you have a whole lot of them. It's intended to use less storage than an int, but that int will have the "natural size" for the architecture. Logically it follows that a short probably doesn't. Similar to bitfields, this means that shorts can be considered a space/time tradeoff. That's usually only worth it if it buys you a whole lot of space. You're unlikely to have very many literals in your application, though, so there was no need foreseen to have short literals. The usecases simply didn't overlap.

    0 讨论(0)
提交回复
热议问题