https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96496

            Bug ID: 96496
           Summary: Conversion to enum with underlying type bool produces
                    wrong result
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fw at gcc dot gnu.org
  Target Milestone: ---

enum E : bool { One, Two };

int
f1 (int x)
{
  return (E) x;
}

The conversion must first be to type bool, according to:

“If the enumeration type has a fixed underlying type, the value is first
converted to that type by integral conversion, if necessary, and then to the
enumeration type.”

<http://eel.is/c++draft/expr.static.cast#10>

And that must produce a comparison against zero:

“A zero value, null pointer value, or null member pointer value is converted to
false; any other value is converted to true.”

<http://eel.is/c++draft/conv.bool#1>

Currently, GCC performs a bit mask (at -O2):

_Z2f1i:
        movl    %edi, %eax
        andl    $1, %eax
        ret

Reply via email to