[Bug c++/96778] New: static cast a integer to a enum type whose underlying type is fixed

fujii.hironori at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Aug 25 02:06:42 GMT 2020


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

            Bug ID: 96778
           Summary: static cast a integer to a enum type whose underlying
                    type is fixed
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fujii.hironori at gmail dot com
  Target Milestone: ---

From
https://developercommunity.visualstudio.com/content/problem/1156511/converting-bool-based-enum-type-to-bool-type.html?childToView=1161122#comment-1161122

> enum LockHistory : bool { Yes, No };
> 
> constexpr bool to_bool(LockHistory e)
> {
>     return e;
> }
> 
> int main()
> {
>     const LockHistory x = static_cast<LockHistory>(2);
>     static_assert(to_bool(x), "fail"); // The static assert fails in GCC, but OK in clang
> }


According to the spec https://en.cppreference.com/w/cpp/language/static_cast

> 8) A value of integer or enumeration type can be converted to any complete enumeration type.
>    (..)
>    If the underlying type is fixed, the result is the same as the
>    converting the original value first to the underlying type of the
>    enumeration and then to the enumeration type.

It should be same with static_cast<LockHistory>(static_cast<bool>(2).

MSVC and Clang can compile the code.
https://godbolt.org/z/315qe6


More information about the Gcc-bugs mailing list