gpt4 book ai didi

c++ - 为什么文字的基数会影响它的类型?

转载 作者:太空宇宙 更新时间:2023-11-04 15:57:37 25 4
gpt4 key购买 nike

十进制数 4294967295 等于十六进制数 0xFFFFFFFF,所以我希望文字具有相同的类型,无论它以什么基数表示,然而

std::is_same<decltype(0xFFFFFFFF), decltype(4294967295)>::value; //evaluates false

看来在我的编译器上 decltype(0xFFFFFFFF)unsigned int,而 decltype(4294967295)signed long.

最佳答案

十六进制文字和十进制文字类型的确定方式不同于 lex.icon table 7

The type of an integer literal is the first of the corresponding list in Table 7 in which its value can be represented.

当十进制文字没有后缀时,列出的类型是按顺序排列的:

integer
long int
long long int

对于十六进制,顺序列表是:

int
unsigned int
long int
unsigned long int
long long int unsigned long long int

为什么会存在这种差异?考虑到我们在 C 中也有这个,我们可以查看 C99 rationale document它说:

Unlike decimal constants, octal and hexadecimal constants too large to be ints are typed as unsigned int if within range of that type, since it is more likely that they represent bit patterns or masks, which are generally best treated as unsigned, rather than “real” numbers.

关于c++ - 为什么文字的基数会影响它的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com