This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/52763] Warning if compare between enum and non-enum type
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 04 Apr 2012 15:00:57 +0000
- Subject: [Bug c++/52763] Warning if compare between enum and non-enum type
- Auto-submitted: auto-generated
- References: <bug-52763-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52763
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-04 15:00:57 UTC ---
(In reply to comment #3)
> (In reply to comment #2)
> > But what about cases such as (val1 == (ONE|TWO)) ?
> >
> > (ONE|TWO) is of type 'int' but that code is correct and shouldn't warn
>
> In my opinion, there should be a warning here, because (ONE|TWO) is not in the
> enumeration range (there is no definition for 3).
It is in range. For enum { NONE = 0, ONE = 1, TWO = 2 } the standard says the
values of the enumeration are in the range 0 to 3, e.g. it could be represented
by a two-bit unsigned integer. The standard sets the rules, not your opinion
:)
Also, that would warn for perfectly valid (and very common) uses of enumeration
types for bitmasks, e.g. std::ios::openmode could be defined as an enumeration
type and you could say
if (mode == (std::ios::in|std::ios::out))
where there is no enumerator defined for in|out, but this code is common and
should not warn.
> If it was defined (e.g. typedef enum {NONE = 0, ONE = 1, TWO = 2, THREE = 3}
> tEnumType), result could again be of the enumeration type and there would be no
> warning.
No, the result would still be an int, ONE|TWO has type int, period.
I think the warning could be useful in some cases, but it needs to be defined
much more carefully than simply "warning each time a enumeration type is
compared to a non enumeration type" as you suggested.