https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
Bug ID: 114537 Summary: bit_cast does not work NSDMI of bitfields Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: FIXME Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Target Milestone: --- Take: ``` #include <bit> struct A { signed char b:1 = 0; signed char b1:7 = 0; }; struct B { unsigned char b; }; static_assert( std::bit_cast<B>(A{}).b == 0 ); ``` This should work as the bitfields span all of the char space but currently we get a sorry: ``` <source>:9:40: error: non-constant condition for static assertion 9 | static_assert( std::bit_cast<B>(A{}).b == 0 ); | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~ In file included from <source>:2: <source>:9:32: in 'constexpr' expansion of 'std::bit_cast<B, A>(A{0, 0})' /opt/compiler-explorer/gcc-trunk-20240331/include/c++/14.0.1/bit:94:33: sorry, unimplemented: '__builtin_bit_cast' cannot be constant evaluated because the argument cannot be encoded 94 | return __builtin_bit_cast(_To, __from); | ^~~ ``` I had been looking for a quick workaround for PR 114536 (for little-endian only) but it looked like NSDMI for bitfields is not implemented fully.