Date: Fri, 16 Jan 2026 15:23:17 +0100
On Fri, 16 Jan 2026 at 15:03, Tiago Freire <tmiguelf_at_[hidden]> wrote:
> I think it would be a foot gun if:
>
>
>
> T1 A {};
>
> T2 B = std::bit_cast<T2>(A);
>
>
>
> static_assert(sizeof(T1) == sizeof(T2));
>
> static_assert(alignof(T1) == alignof (T2));
>
> assert(memcmp(&A, &B, sizeof(T1) ) !=0 );
>
>
>
> It would violate the essential contract of what `std::bit_cast` is used
> for.
>
> There might be an argument to be made that casting between types with a
> different layout of padding bits might not be a good idea.
>
> But I wouldn’t go around changing behavior like this.
>
I don't know what you're trying to show with this code sample. This
contract you're talking about does not exist.
The whole point I'm making is that if T1 has padding bits and T2 does not,
then this std::bit_cast<T2>(A); call is equivalent to std::unreachable(),
regardless of the value A holds.
>
> I think it would be a foot gun if:
>
>
>
> T1 A {};
>
> T2 B = std::bit_cast<T2>(A);
>
>
>
> static_assert(sizeof(T1) == sizeof(T2));
>
> static_assert(alignof(T1) == alignof (T2));
>
> assert(memcmp(&A, &B, sizeof(T1) ) !=0 );
>
>
>
> It would violate the essential contract of what `std::bit_cast` is used
> for.
>
> There might be an argument to be made that casting between types with a
> different layout of padding bits might not be a good idea.
>
> But I wouldn’t go around changing behavior like this.
>
I don't know what you're trying to show with this code sample. This
contract you're talking about does not exist.
The whole point I'm making is that if T1 has padding bits and T2 does not,
then this std::bit_cast<T2>(A); call is equivalent to std::unreachable(),
regardless of the value A holds.
>
Received on 2026-01-16 14:23:31
