C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fixing std::bit_cast padding bit issues

From: Jan Schultke <janschultke_at_[hidden]>
Date: Fri, 16 Jan 2026 15:38:35 +0100
> I haven't checked, but if the bit_cast specification says it's undefined
> behavior if padding bits map onto value representation bits in the
> target type then I think, it should be relaxed to say that the resulting
> value of those bits is unspecified. That is, you should still be able to
> use bit_cast and its result, as long as you either don't use those bits
> or explicitly set them to well defined values (e.g. by masking).
>
> Then, I think, it would be useful introduce a new function that sets the
> padding bits to zero, e.g.:
>
> template< typename T >
> void clear_padding(T& val) noexcept;
>
> which basically matches __builtin_clear_padding. Then that function
> could be combined with bit_cast to make its result deterministic.
>
> The above function is useful on its own, e.g. in atomic implementation
> or to be able to compare structures with padding using memcmp in users'
> code.


That's exactly what David suggested above, though I don't think it's a
viable option. See my previous response.

Received on 2026-01-16 14:38:52