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]

range of enums



Jeffrey Law wrote:

> typedef enum {A=0, B, C, D} T;
> main(){
>   T x;
>   for (x=A; x<=D; ++x)
>     printf("%d ", (int)x);
>   putchar('\n');
>  }
>
> So the compiler could use a 2 bit unsigned field for x since the values for 
> enum T are 0, 1, 2, 3.  Other values will not fit and would be considered
> invalid.  Thus removing the test x <= D is technically valid for C++.  Right?
>
> That (of course) isn't binding for C.

Is it worth pointing out that in fact gcc/egcs does not use any such
bit fields for representing enums, so an argument based on the fact
that it *could* seems somewhat empty?

As long as we know that it's using standard char|int|long int 
types to represent enums isn't it reasonable to have comparisons work
correctly based on this knowledge?  If and when gcc/egcs gets modified 
to use bitfields to represent enums (which seems a somewhat remote
possibility) a lot more work will be necessary to produce the
appropriate checks warnings when an enum is assigned to.




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