Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).

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?

Here's the offending line:

typedef Proto_Int<-2147483648,32> Typeinfo_77;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
347 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

That is why min int is defined as,

#define INT_MIN (-2147483647 - 1)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...