https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117963
Bug ID: 117963
Summary: Invalid enum values accepted in constexpr with
-fstrict-enums
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Keywords: accepts-invalid
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
enum E { E0, E1, E2 };
constexpr E f() { return static_cast<E>(44); }
constexpr E e = f();
GCC accepts this even with -fstrict-enums
Clang correctly diagnoses it (but incorrectly does so twice?):
enum.cc:3:26: error: integer value 44 is outside the valid range of values [0,
3] for the enumeration type 'E' [-Wenum-constexpr-conversion]
3 | constexpr E f() { return static_cast<E>(44); }
| ^
enum.cc:3:26: error: integer value 44 is outside the valid range of values [0,
3] for the enumeration type 'E' [-Wenum-constexpr-conversion]
2 errors generated.
Clang treat it like a pedwarn. It can be disabled with
-Wno-enum-constexpr-conversion or downgraded to a warning with
-Wno-error=enum-constexpr-conversion (they are trying to make the constexpr
cases an unconditional error but are getting pushback from users).