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
323 views
in Technique[技术] by (71.8m points)

c++ - Visual Studio 2015 - Compiler Warning (level 2) C4146

I have the following line in my code

signed int test_case= -2147483648;

which generates the error:

C4146 unary minus operator applied to unsigned type, result still unsigned

but this is still with the data range of teh signed integer type:

__int32 signed, signed int, int –2,147,483,648 to 2,147,483,647

The strange things is assigning it as signed long gives this same error, i.e.

signed long test_case= -2147483648;

The changes below compile OK:

signed int test_case= -2147483647;

signed int test_case= 2147483649;

signed long test_case= -214748364800;
  • Has anyone seen this issue with Visual Studio 2015 compiler?
  • How is it defining the data types?
  • How is the range checked?
  • Why does it seem to ignore the "signed" assignment?

Thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This is a compiler bug.

First thing to note: -2147483648 is not a literal. There is no such thing as a negative literal in C++.

-2147483648 is a compile time evaluable constant expression consisting of 2147483648 and the unary minus operator.

On MSVC targeting Windows x64 (where an int and long are both 32 bit), 2147483648 should be a long long int, and therefore so will -2147483648. My understanding is that the standard insists on a signed type unless you use a hexadecimal or octal literal.

The narrowing conversion to signed int is, in this case, well-defined since you're targeting a platform with a 32 bit 2's complement int type.

Further reference: see http://en.cppreference.com/w/cpp/language/integer_literal


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

...