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

时间:2017-02-21 16:25:57

标签: c++ types overloading literals

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

1 个答案:

答案 0 :(得分:3)

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

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

同样,对于floating literals,没有后缀为您提供doublef为您提供floatl为您提供long double。< / p>

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

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

5_u16 // uint16_t

除了VC显然为所有类型提供了字面后缀 - 例如10i16为您提供int16_t

相关问题