Date: Wed, 21 Jan 2026 18:45:45 +0100
Upon second thought, I do think there's a way to make the idiom work with a
std::bit_cast that only clears input padding:
using Tmp = std::array<unsigned char, sizeof(T)>;
Tmp tmp = std::bit_cast<Tmp>(src); // or std::bit_cast_clear_padding
std::memcpy(src, &tmp, sizeof(T));
Since byte arrays have no padding bits, there is no issue with padding bits
in the std::bit_cast result. It's crucial to then memcpy back into src
rather than using the assignment operator; in that case, I think this can
clear padding in the source.
On Wed, 21 Jan 2026 at 18:41, Jan Schultke <janschultke_at_[hidden]>
wrote:
>
> > Because the result of std::bit_cast has unspecified padding bits, so x =
>>
>> Unless we change std::bit_cast, which is what I am proposing.
>
>
> It would be an ABI break if you changed that, and compilers would
> essentially have to invent new ABI for all kinds of platforms to make this
> work, so the std::bit_cast that you're proposing is practically
> unimplementable.
>
std::bit_cast that only clears input padding:
using Tmp = std::array<unsigned char, sizeof(T)>;
Tmp tmp = std::bit_cast<Tmp>(src); // or std::bit_cast_clear_padding
std::memcpy(src, &tmp, sizeof(T));
Since byte arrays have no padding bits, there is no issue with padding bits
in the std::bit_cast result. It's crucial to then memcpy back into src
rather than using the assignment operator; in that case, I think this can
clear padding in the source.
On Wed, 21 Jan 2026 at 18:41, Jan Schultke <janschultke_at_[hidden]>
wrote:
>
> > Because the result of std::bit_cast has unspecified padding bits, so x =
>>
>> Unless we change std::bit_cast, which is what I am proposing.
>
>
> It would be an ABI break if you changed that, and compilers would
> essentially have to invent new ABI for all kinds of platforms to make this
> work, so the std::bit_cast that you're proposing is practically
> unimplementable.
>
Received on 2026-01-21 17:45:59
