Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

c++ - How do I define a constant equal to -2147483648?

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

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

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...