This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/46457] New: Bogus warning about bitwise combination of enum flags in case statement
- From: "bhutchings at solarflare dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 12 Nov 2010 22:12:52 +0000
- Subject: [Bug c++/46457] New: Bogus warning about bitwise combination of enum flags in case statement
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46457
Summary: Bogus warning about bitwise combination of enum flags
in case statement
Product: gcc
Version: 4.5.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: bhutchings@solarflare.com
The -Wswitch warning option is documented as resulting in warnings about
"`case' labels outside the enumeration range". This appears to be a change from
gcc 4.4. I have no objection to the documented behaviour, but the actual
behaviour is to warn about any value that does not equal an enumerator.
It is common practice to define flags as enumerators and to combine them with
the '|' operator. The C++98 standard supports this practice by requiring that
the underlying type for an enumerated type must be capable of storing such
bitwise combinations and zero (7.2p6).
Test case:
enum flags {
one = 1,
two = 2,
};
const char *f(enum flags fl)
{
switch (fl) {
case 0:
return "zero";
case one:
return "one";
case two:
return "two";
case one | two:
return "three";
default:
return "bogus";
}
}
$ c++ -c -Wall test.cc
test.cc: In function âconst char* f(flags)â:
test.cc:9:5: warning: case value â0â not in enumerated type âflagsâ
test.cc:15:5: warning: case value â3â not in enumerated type âflagsâ
This also applies to the C front-end, though the C standard does not explicit
specify this requirement on the underlying type (or "compatible type").