This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]