How to write an unsigned short int literal?

后端 未结 9 1400
慢半拍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(23);
    

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

    func( static_cast(23) );
    

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

    func( 23 );
    

提交回复
热议问题