【发布时间】:2016-05-19 16:50:41
【问题描述】:
这是我的代码
#include <iostream>
static const unsigned long long int xx = (36 * 36 * 36 * 36) * (36 * 36 * 36 * 36);
static const unsigned long long int y = 36 * 36 * 36 * 36;
static const unsigned long long int yy = y * y;
int main()
{
std::cout << xx << std::endl;
std::cout << yy << std::endl;
return 0;
}
这是编译输出
# g++ -std=c++11 test.cpp -o test
test.cpp:2:62: warning: integer overflow in expression [-Woverflow]
static const unsigned long long int xx = (36 * 36 * 36 * 36) * (36 * 36 * 36 * 36);
这是执行输出
# ./test
18446744073025945600
2821109907456
您能解释一下为什么我会看到此警告和不同的结果吗?如果 36 可以适合 char 那么 36^8 可以适合 unsigned long long int 所以我不确定这里有什么问题,请告知。 (我使用的是 gcc 4.9.2)
【问题讨论】:
-
这似乎是一个非常普遍的误解:后来的意图以某种方式影响了早期代码的含义。 (这里的“earlier”和“later”指的是子表达式包含顺序。)C++ 中通常不是这种情况;最接近这样的效果的是在强制转换中的地址操作数上的重载决议。