Help with bit-field semantics in C and C++

Joe Buck Joe.Buck@synopsys.COM
Wed Aug 25 00:21:00 GMT 2004


Joe Buck <Joe.Buck@synopsys.COM> writes:
> | I agree with you so far; (E)n might be 7.

On Wed, Aug 25, 2004 at 01:29:50AM +0200, Gabriel Dos Reis wrote:
> I disagree that the enum object would have value 7, as 7 is not an
> enumeration (E) value.

I attempted to clarify this in the followup; I meant to say that an
implementation might produce code that still has a 7 at that point,
because the behavior of the program is unspecified.  Or are you claiming
that the compiler is *required* to emit code that will transform the 7
into one of [0,3]?

> | The next question, then, is whether, despite this, a compiler is allowed
> | to implement a switch on a variable of type E as a jump table, in such a
> | way that a value outside of [0,3] malfunctions (jumps to a random address).
> 
> By the type properties confered to E, any value of that type is in the
> range [0,3]. 

For a conforming program.  Here we have a program that attempts to assign
an out-of-range value to an enum.

> | Let's leave to one side whether this is a good idea.  Is it permitted?
> 
> That can't happen.

But it does happen.  Is that a bug we must fix?  If so, we have to slow
down the compiler to prevent any conversion from an int to an enum from
assigning an out-of-range value.



More information about the Gcc mailing list