https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117382
Bug ID: 117382 Summary: Conversion of integral to enumeration outside of range is UB but works in consteval context Product: gcc Version: 14.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: konstantin.vladimirov at gmail dot com Target Milestone: --- Consider this code: --- enum class MyPrettyEnum { Value = 0, }; consteval int process(int n) { MyPrettyEnum E = static_cast<MyPrettyEnum>(n); return static_cast<int>(E); } constexpr int x = process(10); --- in C++23 [expr.static.cast] / 10 If the enumeration type does not have a fixed underlying type, the value is unchanged if the original value is within the range of the enumeration values (9.7.1), and otherwise, the behavior is undefined. [expr.const] / 5 An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine (6.9.1), would evaluate one of the following: [...] — an operation that would have undefined behavior So this shall be prohibited.