enum MyEnum : short { ENUM_ELEMENT = SHRT_MIN } = Warning C4341 ???

Am I missing something here I'm using VS 2005, and this:

enum MyEnum : short

{

ENUM_ELEMENT = SHRT_MIN

};

Results in this:

warning C4341: 'ENUM_ELEMENT' : signed value is out of range for enum constant


SHRT_MIN is defined in limits.h as:

#define SHRT_MIN (-32768) /* minimum (signed) short value */

BTW, I tried SHRT_MIN+1 and got the same thing.



Answer this question

enum MyEnum : short { ENUM_ELEMENT = SHRT_MIN } = Warning C4341 ???

  • Christian_42

    It might be two compiler bugs.  My preliminary question is "are you compiling this under C++/CLI (compiling C++ with /clr switch) "

    According to this version of the C++ (not C++/CLI) standard, enum declarations cannot be specified with a different underlying type, as you're doing here with short.  So bug #1 would be that the C++ compiler is not catching this syntax error.

    Underlying enumeration types are a feature with the "enum class" declaration (aka managed enumeration), not "enum" (unmanaged enumeration). 

    So it's possible that this syntax is illegal also under C++/CLI.  The MSDN docs are a bit confusing on the matter.  It states: "If a standard C++ enum is defined (without class or struct), compiling with /clr will cause the enumeration to be compiled as a managed enum. The enumeration still has the semantics of an unmanaged enumeration."  My interpretation of this is that this particular enum is visible in the CLS type system, but does not support underlying types (i.e. it always takes on the default type.)

    Bug #2 relates specifically to your question.  With "enum class id : type" under C++/CLI, one would expect that it should accept negative enumerated values for the underlying type short, since the default underlying type of int does allow them. 

    You should open a bug on this in the Product Feedback area..


  • enum MyEnum : short { ENUM_ELEMENT = SHRT_MIN } = Warning C4341 ???