Help with bit-field semantics in C and C++
Mark Mitchell
mark@codesourcery.com
Tue Aug 24 05:06:00 GMT 2004
Roger Sayle wrote:
>Could one of GCC's resident C/C++ language lawyers confirm that the
>behaviour of the following code is undefined?
>
>
>extern "C" void abort(void);
>
>enum E { e = 0x0f };
>
>void test(int n)
>{
> enum E x, y;
>
> x = (E)0x10;
>
>
That conversion is unspecified in C++, as others have said.
Now, the next question is what behavior we should prefer. I think that
we should avoid generating any code for such a conversion, so that means
that we should avoid, for example, generating mask instructions. As
Joseph points out, it's not crystal clear whether the result of the
conversion must result in a valid value of the underlying enumeration
type -- but all compilers of which I am aware will just store "0x10" in
the memory for "x" without doing any truncation, so I think that's what
we should do too.
In other words, treat this like a conversion to the underlying integer
type for the enumeration.
--
Mark Mitchell
CodeSourcery, LLC
(916) 791-8304
mark@codesourcery.com
More information about the Gcc
mailing list