Why does MSVC pick a long long as the type for -2147483648?(为什么 MSVC 选择 long long 作为 -2147483648 的类型?)
问题描述
我的片段:
auto i = -2147483648;
int j = 3;
std::swap(i, j); // Compile error about mismatched types here.
编译器声明文字 i 是一个 long long.这是为什么?-2147483648 适合 MSVC x64 上的 int.
The compiler states that the literal i is a long long. Why is that? -2147483648 fits in an int on MSVC x64.
我的编译器是 MSVC,目标是 64 位.
My compiler is MSVC, target is 64 bits.
推荐答案
与普遍看法相反,-2147483648 不是一个字面量:C++ 不支持负字面量值.
Contrary to popular belief, -2147483648 is not a literal: C++ does not support negative literal values.
事实上,它是一个编译时可评估的常量表达式,由文字 2147483648的一元否定组成.
It is, in fact, a compile-time evaluable constant expression consisting of a unary negation of the literal 2147483648.
在具有 32 位 ints 和 longs 的 MSVC x64 上,2147483648 对于其中任何一个来说都太大,因此它会故障转移到 long long 你观察到的类型.
On MSVC x64, which has 32 bit ints and longs, 2147483648 is too big for either of those so it fails over to the long long type that you observe.
这篇关于为什么 MSVC 选择 long long 作为 -2147483648 的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 MSVC 选择 long long 作为 -2147483648 的类型?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
