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

标签 c++ integer literals

十进制数 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/

相关文章:

python - 如何检查一个大的 float 是否是一个整数?

python - 什么是常量和文字常量?

c++ - 如何在 Visual Studio 2008 中创建自定义清理(清理后)事件?

c++ - 对于所有编译器,C++ 中的 If 语句中的各种条件是否总是具有相同的执行顺序?

java - String.format() 获取最大值

javascript - 固定整数 Google 脚本的字符串数字

perl - 取消缩进多行字符串文字

ios - 文字 @YES 在 iOS 5/Xcode 4.4 中不起作用

c++ GetDIBits 不工作

c++ - g++ 版本 4.0.0.8 和 4.3.2 之间有什么区别?