This is the mail archive of the egcs@egcs.cygnus.com mailing list for the EGCS project. See the EGCS home page for more information.


[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

Re: [PATCH] Fix for short-enums comparison bug




> OK.  Thanks.  So if we go back to the original sample code:
> 
> 
> 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?

Hmm.  I'm not sure.  It would seem so.  It does seem that the code is
risky, at least, and might merit a warning (I definitely think that
g++ must not remove this test without issuing a warning).

Note also that the operators =, <=, and ++ can be overloaded for an
enum, which would change things yet again (I could define ++ to wrap
around if I wanted to).

> That (of course) isn't binding for C.

Yes, in C enums are basically ints of some form.  However, even in C
there might be similar cases, if one of the enum values is the maximum
value of the integral type that is used.