作者:凡妮04 | 来源:互联网 | 2022-10-30 16:55
我在哪里可以找到所有C ++十进制类型指示符,例如
long l = 0L;
我也知道LU f d。还有其他吗?我在哪里可以找到它们?如何明确指示未签名的短裤?
1> Egor Shkorov..:
您引用的该指标称为后缀。
对于整数类型,有两种类型的后缀:
无符号后缀 —字符u
或字符U
long后缀 -字符l
或字符L
或long-long后缀 -字符序列ll
或字符序列LL
。
对于整数文字,可以组合使用这些后缀,例如ul
或,ull
以在同一文字中同时实现“ unsignednes”和“ longness”。
还有用于浮点类型后缀:一f
,F
,l
,或者L
没有后缀的文字定义 double
f
或F
定义float
l
或L
定义long double
也有用户定义的文字,您可以为其引入用户定义的后缀。
至于您的第二个问题unsigned short
:,没有明确的后缀short
,因此您必须使用static_cast
或C样式强制转换。
另一种方法是像这样定义用户定义的文字运算符
unsigned short operator "" _ush(unsigned long long int a)
{
return static_cast(a);
}
然后使用它来定义如下文字: unsigned short a = 123_ush;
我检查了它是否可以使用以下代码段:
#include
#include
#include
unsigned short operator "" _ush(unsigned long long int a)
{
return static_cast(a);
}
int main()
{
std::string name;
bool equal = typeid(decltype(123_ush)) == typeid(unsigned short); // check that literal is indeed unsigned short
std::cout <
有关我的答案中提到的内容的更多信息,我建议您检查cppreference:整数,浮点文字,用户定义的文字
太好了,我喜欢这个。