[PATCH] c++: Add __builtin_bit_cast to implement std::bit_cast [PR93121]

Jakub Jelinek jakub@redhat.com
Thu Aug 27 10:46:56 GMT 2020

On Thu, Aug 27, 2020 at 12:06:13PM +0200, Jakub Jelinek via Gcc-patches wrote:

Oops, rewrote the testcase from __builtin_bit_cast to std::bit_cast without
adjusting the syntax properly.
Also, let's not use bitfields in there, as clang doesn't support those.
So, adjusted testcase below.  clang++ rejects all 6 of those, but from what
you said, I'd expect that u and z should be well defined.

#include <bit>

struct S { short a; int b; };
struct T { int a, b; };

constexpr int
foo ()
  S a = S ();
  S b = { 0, 0 };
  S c = a;
  S d;
  S e;
  d = a;
  e = S ();
  int u = std::bit_cast<T> (a).a; // Is this well defined due to value initialization of a?
  int v = std::bit_cast<T> (b).a; // But this is invalid, right?  There is no difference in the IL though.
  int w = std::bit_cast<T> (c).a; // And this is also invalid, or are default copy ctors required to copy padding bits?
  int x = std::bit_cast<T> (d).a; // Similarly for default copy assignment operators...
  int y = std::bit_cast<T> (e).a; // And this too?
  int z = std::bit_cast<T> (S ()).a; // This one is well defined?
  return u + v + w + x + y + z;

constexpr int x = foo ();


More information about the Gcc-patches mailing list