Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > enum as bitfields

Reply
Thread Tools

enum as bitfields

 
 
Harald van Dijk
Guest
Posts: n/a
 
      10-01-2011
On Oct 1, 11:21*pm, Quentin Pope <(E-Mail Removed)> wrote:
> On Sat, 01 Oct 2011 06:37:23 -0400, James Kuyper wrote:
> > On 10/01/2011 04:59 AM, Quentin Pope wrote:
> >> On Thu, 29 Sep 2011 22:11:18 +0100, Ben Bacarisse wrote:
> >>> Quentin Pope <(E-Mail Removed)> writes:
> >>> * A bit-field shall have a type that is a qualified or unqualified
> >>> * version of _Bool, signed int, unsigned int, or some other
> >>> * implementation-defined type.

>
> >>> which, if violated would make your program undefined and would require
> >>> a diagnostic. *However, from the wording it would seem that an
> >>> implementation that documents the possibility of using an enum, may
> >>> accept this code without saying a word.

>
> >> So in effect, it may or may not be a constraint violation, depending on
> >> whether the underlying type of the enum is int, unsigned int or
> >> something else?

>
> > No - the underlying type of the enum is irrelevant. [...]

>
> > Of course, portable code should only use the types that are guaranteed
> > to be allowed.

>
> James
>
> I think you may have misunderstood what I was saying.
>
> My point was that if the enum type is an alias for int, then that is one
> of the allowed types for a bitfield with no need for any implementation
> documentation.


An enum type is never an alias for int. If the underlying type is int,
then the enum type is compatible with int, but it is still a different
type, in much the same way that int[] and int[3] are compatible but
different types. Does this affect programs aside from bit-fields? I
believe so:

#include <limits.h>
enum INT1 { MIN1 = INT_MIN, MAX1 = INT_MAX }; /* Assume INT1 is
compatible with int */
enum INT2 { MIN2 = INT_MIN, MAX2 = INT_MAX }; /* Assume INT2 is
compatible with int too */

int a;
enum INT1 a; /* Valid. int and enum INT1 are compatible */

int b;
enum INT2 b; /* Also valid. int and enum INT2 are compatible */

enum INT1 c;
enum INT2 c; /* Constraint violation. enum INT1 and enum INT2 are not
compatible. */

Actually, this is something that the usually strictest compiler I have
gets wrong, which accepts even the last declaration. GCC and ICC get
it right and reject it.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-01-2011
On 10/01/2011 05:21 PM, Quentin Pope wrote:
> On Sat, 01 Oct 2011 06:37:23 -0400, James Kuyper wrote:
>
>> On 10/01/2011 04:59 AM, Quentin Pope wrote:
>>> On Thu, 29 Sep 2011 22:11:18 +0100, Ben Bacarisse wrote:
>>>
>>>> Quentin Pope <(E-Mail Removed)> writes:

....
>>>> Yes, it does. There is a constraint:
>>>>
>>>> A bit-field shall have a type that is a qualified or unqualified
>>>> version of _Bool, signed int, unsigned int, or some other
>>>> implementation-defined type.
>>>>
>>>> which, if violated would make your program undefined and would require
>>>> a diagnostic. However, from the wording it would seem that an
>>>> implementation that documents the possibility of using an enum, may
>>>> accept this code without saying a word.
>>>
>>> So in effect, it may or may not be a constraint violation, depending on
>>> whether the underlying type of the enum is int, unsigned int or
>>> something else?

>>
>> No - the underlying type of the enum is irrelevant. What is relevant is
>> the implementation's documentation: any additional types that the
>> documentation says are acceptable may be used. Since "A bit-field is
>> interpreted as a signed or unsigned integer type" (6.7.2.1p9), it
>> wouldn't make much sense for an implementation to accept non-integral
>> types. However, as far as the constraint listed above is concerned, if
>> an implementation were to document that it accepts 'double' as a type
>> for a bit-field, then even "double f:16" would not be a violation of the
>> constraint listed above.
>>
>> Of course, portable code should only use the types that are guaranteed
>> to be allowed.

>
> James
>


> I think you may have misunderstood what I was saying.


Apparently.

> My point was that if the enum type is an alias for int, then that is one


An enum type is not an alias for an int. In C, types are aliased only
through the typedef mechanism. Every enumerated type is required to be
"compatible with char, a signed integer type, or an
unsigned integer type" (6.7.2.2p4), but compatible types are not aliases
for each other. The standard often allow one type to be used in place of
a compatible type, but that's only true when the standard explicitly
says so; this is not one of those cases.

> of the allowed types for a bitfield with no need for any implementation
> documentation.


I didn't realize that this was the point you were making, in part
because your point is incorrect. An enumerated type that's compatible
with 'int' does not qualify as actually being an 'int' for this purpose
(except insofar as a particular implementation allows it).

> So although the code won't be portable to every implementation, it will
> still be portable (no constraint violation) for all implementations where
> enums have type int.


A conforming implementation could make your enumeration compatible with
signed int, and still refuse to compile such a program by reason of
violating that constraint.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments