public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106294] New: GCC accepts the undefined behavior operation in a constant expression
@ 2022-07-14 12:15 xmh970252187 at gmail dot com
  2022-07-15 21:52 ` [Bug c++/106294] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: xmh970252187 at gmail dot com @ 2022-07-14 12:15 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/106294] GCC accepts the undefined behavior operation in a constant expression
  2022-07-14 12:15 [Bug c++/106294] New: GCC accepts the undefined behavior operation in a constant expression xmh970252187 at gmail dot com
@ 2022-07-15 21:52 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-15 21:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
All compilers I tried (GCC, clang and MSVC) all accept this. There might be a
defect report in this area too.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-15 21:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-14 12:15 [Bug c++/106294] New: GCC accepts the undefined behavior operation in a constant expression xmh970252187 at gmail dot com
2022-07-15 21:52 ` [Bug c++/106294] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).