为什么文字的基数会影响它的类型?
Why does the base of a literal affect its type?
十进制数 4294967295 等于十六进制数 0xFFFFFFFF,所以无论基数是什么,我都希望文字具有相同的类型它表示为,yet
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.
十进制数 4294967295 等于十六进制数 0xFFFFFFFF,所以无论基数是什么,我都希望文字具有相同的类型它表示为,yet
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.