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: unassig...@gcc.gnu.org
        ReportedBy: bhutchi...@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").

Reply via email to