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;
Thanks
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With