本文介绍了如何定义一个常数等于-2147483648?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码由脚本生成,代码涉及一些整数常量。我遇到了一个奇怪的问题:其中一个常量等于-2147483648(最小签名int32),Visual Studio生成以下错误一元减运算符应用于无符号类型,结果仍然是unsigned (这是特别奇怪,因为我没有治疗警告作为错误启用)。

I have a chunk of code generated by a script, and the code involves some integer constants. I ran into a strange problem: one of the constants is equal to -2147483648 (minimum signed int32), and Visual Studio generates the following error: unary minus operator applied to unsigned type, result still unsigned (which is especially strange since I don't have treat warnings as errors enabled).

显然,它看到一个整数不适合在signed类型,使它无符号,只有然后应用一元减。我想结果仍然是正确的,因为它将被转换为 int (这实际上是一个模板参数),但我想避免的警告。

Apparently it sees an integer that doesn't fit in the signed type, makes it unsigned, and only then applies the unary minus. I suppose the result would still be correct as it will be casted to int (which is actually a template argument), but I'd like to avoid the warning. Is it just a VS feature or does the standard have anything to say about it?

这是冒犯行:

typedef Proto_Int<-2147483648,32> Typeinfo_77;


推荐答案

在C ++中,负数以正数存储 - )负号。

In C++ negative numbers are stored as positive number with (-) negative sign.

这就是为什么min int定义为

That is why min int is defined as,

#define INT_MIN 2147483647 - 1)

对于int C ++不理解 2147483648 。因此,如果可能,您还可以将 2147483648 写为( - 2147483647 - 1)

For int C++ does not understand 2147483648. So if possible you can also write 2147483648 as (-2147483647 - 1)

这篇关于如何定义一个常数等于-2147483648?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:00