Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Is C++ Issuing Warnings On Enums With Negative Values?

My current code resembles this:

enum Enum1 : signed __int8
{
    Value1 = 1 ,
    Value2 = 2 ,
    Value3 = -3  // C4341
} ;

The error details state:
"warning C4341: 'Value3' : signed value is out of range for enum constant"

MSDN states that this warning only occurs when you use values outside of the range of an int:
(> 2^31) OR (< 1 - 2^31)

Why is it telling me that my negative values are invalid? It is only a warning, but Google is telling me that this warning indicates that these enum values will be undefined - which will break my program.

like image 661
Giffyguy Avatar asked Feb 25 '23 22:02

Giffyguy


2 Answers

Seems like a bug to me. The current 0x draft doesn't indicate that this should be the case, and neither does the MSDN documentation.

like image 51
Steve M Avatar answered Mar 04 '23 08:03

Steve M


Your answer is basically described here: Are C++ enums signed or unsigned?

It is up to your compiler's implementation to define whether the value of the enum is signed or unsigned. I assume they left the warning there so that if you or someone else uses a different compiler, you may get different behavior.

like image 30
Khalos Avatar answered Mar 04 '23 08:03

Khalos