Shorthand 将类型分配给原始文字的方法

Shorthand way of assigning type to primitive literal

在没有强制转换的情况下将类型分配给文字基元的方法有哪些? 我知道 0.0 变成了 double 而 0.0f 变成了 float。还有其他方法可以对文字进行类型转换吗?

有一堆 suffixes 可以添加到整数文字中以获得所需的类型:

1    // int
2u   // unsigned int
3ll  // long long int
4ull // unsigned long long int

同样,对于floating literals,没有后缀给你doublef给你floatl给你long double .

小于 int 的类型没有预先打包的文字后缀。但是你可以自己写,例如:

inline uint16_t operator ""_u16(uint64_t value) {
    return static_cast<uint16_t>(value);
}

5_u16 // uint16_t

Except VC 显然为所有类型提供了文字后缀 - 就像 10i16 给你一个 int16_t