4

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.

Giffyguy
  • 20,378
  • 34
  • 97
  • 168

4 Answers4

3

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.

Steve M
  • 8,246
  • 2
  • 25
  • 26
2

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.

Community
  • 1
  • 1
Khalos
  • 2,335
  • 1
  • 23
  • 38
  • 1
    I thought adding ": signed __int8" specified what underlying type the compiler would use ... this notation is specific to Visual Studio, and I don't think any compiler specific implementations would override this. – Giffyguy Nov 04 '10 at 04:46
  • It looks like he is using the new c++0x syntax, so I would assume Microsoft should allow you to do this. I'm curious if it shows the warning if he uses char instead of __int8. (PS. It's not VS specific, gcc has it too) – Jason Iverson Nov 04 '10 at 04:49
  • @JasonIverson Yes, the warning is raised both for `char` and even `signed char` – Paolo M Oct 20 '15 at 09:58
2

I am using Microsoft Visual Studio 2010. Using "char" or "signed char" in place of "signed __int8" yields identical results - namely C4341 for "-3" and C4369 for 0xFD
This other MSDN article states clearly that the ": signed __int8" explicitly specifies the underlying type for the enumerators. This being said, it can be assumed that this warning exists simply for the purpose of making the developer aware of possible incompatibilities with other compilers.

Giffyguy
  • 20,378
  • 34
  • 97
  • 168
1

Since someone mentioned it is compiler specific, it works with g++ as shown below

#include <stdio.h>

enum Enum1
{
    Value1 = 1 ,
    Value2 = 2 ,
    Value3 = -3
} ;

enum Enum1 myval;


main(){
    myval = Value3;
    printf("%d\n", myval);
}

compile: g++ negenum.cpp

result when running: -3

Simon
  • 31,675
  • 9
  • 80
  • 92
lemic
  • 177
  • 4