C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Mandate layout of std::array

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 30 Jun 2023 22:43:59 -0400
On Fri, Jun 30, 2023 at 9:34 PM Julien Villemure-Fréchette via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> If I remember correctly , std::array is an aggregate type that directly wraps a single data member of a built-in array type. Although it may not explicitly stated that it has the same layout as a C style array, this property should be deductible from other explicitly state clauses.

Even if that was how `array` was specified, it would still be
insufficient for doing `bit_cast`ing. To do a `bit_cast`, the source
and destination types must be the same size. And there is no
requirement that a struct with a single public member of type
`std::byte[size]` must be precisely `size` bytes in size. The array
will be exactly that big, but the *struct* can be padded.

So to make the `bit_cast` work, you must have assurances that
`array<byte, sizeof(T)>` is exactly `sizeof(T)` in size.

Received on 2023-07-01 02:44:09