This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Range of enums
- To: law at cygnus dot com
- Subject: Re: Range of enums
- From: Mark Mitchell <mark at markmitchell dot com>
- Date: Sat, 13 Feb 1999 09:31:09 -0800
- CC: cgw at alum dot mit dot edu, egcs at cygnus dot com
- References: <11101.918893028@hurl.cygnus.com>
- Reply-to: mark at markmitchell dot com
>>>>> "Jeffrey" == Jeffrey A Law <law@hurl.cygnus.com> writes:
Jeffrey> In message <14020.22010.51278.609918@janus.pgt.com>you
Jeffrey> write:
>> I think this is a not-too-uncommon idiom in C to step through
>> all the values of an enum. As far as I know this is legal C;
>> I'd really like to know if it isn't.
Jeffrey> That's precisely what we're trying to determine. Whether
Jeffrey> or not the code is valid has a significant effect on how
Jeffrey> we approach the problem.
My reading of the C9X draft is that the construct in question *is*
legal, provided the smallest integer type is at 3 bits:
6.5.2.2:
Each enumerated type shall be compatible with an integer type.
The choice of type is implementation-defined, but shall be capable
of representing the values of all the members of the enumeration.
6.1.2.6:
Two types are "compatible" if their types are the same.
If other words, each enumerated type is "the same" as an integer
type. Since our smallest integer type has 8 bits, the example posted
should work, using GCC.
C++ has different language:
7.2:
For an enumeration where e_min is the smallest enumeration and
e_max is the largest, the values of the enumeration are the values
of the underlying type in the range b_min to b_max, where b_min
and b_max are, respectively, the smallest and largest values of
the smallest bit-field that can store e_min and e_max.
So, given:
enum E { a = 2, b = 6 };
the values are actually { 0, 1, 2, 3, 4, 5, 6, 7 }, since the smallest
bit-field that holds 6 is actually 3 bits. Thus, if the original
posters enumeration did not have a maximum value that was one less
than a power of two, the code would have been legal C++ as well.
But, I think the C9X language means that GCC is going to have to do what
the poster expects for C9X. There's no reason to be incompatible
between the various C-like languages; all of them certainly permit the
looser C9X interpretation.
The original patch looked reasonable to me, FWIW.
--
Mark Mitchell mark@markmitchell.com
Mark Mitchell Consulting http://www.markmitchell.com