Subject: Re: Bitfields and kernel
To: Steven J. Dovich <dovich@lethe.tiac.net>
From: m. k. buelow <token@mayn.de>
List: tech-kern
Date: 09/28/1999 23:47:09
Steven J. Dovich wrote:

>The problem with this is that ANSI C does not define the behavior for
>enums as bit masks. The C++ committee ran into this and departed from
>C in this respect because there was significant use of the construct
>in the IOSTREAMS classes. As a result, C++ guarantees sufficient storage
>to represent values between the enum-max and enum-min values. ANSI C on
>the other hand permits the compiler to do all manner of value compression
>to minimize the bits necessary to represent the enumerated values. As
>a result, unless the enum type contains an enumerated symbol for the
>value, you can't rely on the language to store the value in an object
>of that type.

I thought enums were treated as integral types?
If I have "enum { SOMEFLAG = (1<<2) };" how else could SOMEFLAG
be interpreted but with 4?  
The ANSI C rationale says:

3.1.3.3  Enumeration constants

Whereas an enumeration variable may have any integer type that correctly
represents all its values when widened to int, an enumeration constant is
only usable as the value of an expression.  Hence its type is simply int.
(See 3.1.2.5.)  

So the compiler always treats SOMEFLAG as (int)SOMEFLAG, or am I
missing something?

In C++, the "enum kludge" can be avoided entirely, with real constants
of course.

mkb