Strange enum type conversion

Patrick Horgan phorgan1@yahoo.com
Tue Dec 8 23:19:00 GMT 2009


Hajo Münzer wrote:
> Is this defined somewhere in the C++ standard (where?) or is this gcc
> specific? I was told that e. g. in MS Visual C++ 0xffffffff is
> interpreted as -1.
>   
In ISO/IEC 9899:201x (i.e. C99)

Under 6.25 Types

16 An enumeration comprises a set of named integer constant values. Each 
distinct
   enumeration constitutes a different enumerated type.

Under 6.7.2.2 Enumeration specifiers

4 Each enumerated type shall be compatible with char, a signed integer 
type, or an
  unsigned integer type. The choice of type is 
implementation-defined,113) but shall be
  capable of representing the values of all the members of the 
enumeration. The
  enumerated type is incomplete until after the } that terminates the 
list of enumerator
  declarations.
113) An implementation may delay the choice of which integer type until 
all enumeration constants have
     been seen.

Which obviously leaves much up to the compiler.

In ISO/IEC 14882, Programming language – C++ (from a ballot of October 
2008).

Under 3.9.1 Fundamental types

7 Types bool, char, char16_t, char32_t, wchar_t, and the signed and 
unsigned integer types are collectively
  called integral types.44
44) Therefore, enumerations (7.2) are not integral; however, 
enumerations can be promoted to integral types as specified in 4.5.

Under 4.5 Integral promotions

2 ...
                                                    An rvalue of an 
unscoped enumeration type (7.2) can be
  converted to an rvalue of the first of the following types that can 
represent all the values of the enumeration
  (i.e. the values in the range bmin to bmax as described in 7.2): int, 
unsigned int, long int, unsigned
  long int, long long int, or unsigned long long int. If none of the 
types in that list can represent all
  the values of the enumeration, an rvalue of an unscoped enumeration 
type can be converted to an rvalue of
  the extended integer type with lowest integer conversion rank (4.13) 
greater than the rank of long long in
  which all the values of the enumeration can be represented. If there 
are two such extended types, the signed
  one is chosen.

So as you see it's clearly specified in C++.

Patrick



More information about the Gcc-help mailing list