From mboxrd@z Thu Jan 1 00:00:00 1970 From: Joe Buck To: law@cygnus.com Cc: jbuck@Synopsys.COM, gavin@cygnus.com, cgw@alum.mit.edu, egcs@egcs.cygnus.com Subject: Re: [PATCH] Fix for short-enums comparison bug Date: Sun, 28 Feb 1999 22:53:00 -0000 Message-ID: <199902112006.MAA18585@atrus.synopsys.com> References: <6271.918762821@hurl.cygnus.com> X-SW-Source: 1999-02n/msg00395.html Message-ID: <19990228225300.dlSBNekMWmytjnLDLJ0awcKedMX_KyGZcUu1-dF-0Iw@z> > 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.