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

            Bug ID: 106294
           Summary: GCC accepts the undefined behavior operation in a
                    constant expression
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xmh970252187 at gmail dot com
  Target Milestone: ---

````cpp
enum A {a = 0};
constexpr A e = static_cast<A>(1024);
````
According to [expr.static.cast p10
> 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 
> ([dcl.enum]), and **otherwise, the behavior is undefined**.

[dcl.enum] p8 defines the range of the enumeration values, which says
> Otherwise, the values of the enumeration are the values representable by a 
> hypothetical integer type with **minimal** width M such that all enumerators 
> can be represented.  

In this case, it is sufficient to represent the value `0` of the unique
enumerator if `M` is `1`. Apparently, the value `1024` cannot be representable
in the hypothetical integer type with minimal width M. So, the full-expression
of the initialization is not a constant expression since the violation of
[expr.const] p5
> an operation that would have undefined behavior as specified in [intro] 
> through [cpp];

However, GCC accepts this example without any diagnosis.
  • [Bug c++/106294] New: GCC accep... xmh970252187 at gmail dot com via Gcc-bugs

Reply via email to