C++ Logo


Advanced search

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

From: Brian Bi <bbi5291_at_[hidden]>
Date: Fri, 30 Jun 2023 20:34:21 -0400
Would folks be interested in a proposal to require std::array<T, N> to have
the exact same layout as T[N], i.e., no padding before or after the
internal T[N] data member?

(The special case where N is zero would be excluded from the requirement,

A lot of people are surprised when I tell them that the standard doesn't
already provide this guarantee.

I can think of some theoretical reasons why this would be useful, such as
being able to serialize a T[N] and then deserialize it as std::array<T, N>
using the `std::start_lifetime_as` function. However, I'd like to hear from
folks here if they would actually benefit from this guarantee in a real
application. If there's sufficient motivation, I'll write a paper.

I don't think implementations would actually have to do any work to conform
to this requirement, so my only concern is about whether I can provide
sufficient motivation to get this proposal through the committee.

(Bonus: require std::array<T, N> to be pointer-interconvertible with its
T[N] member, even when T is not standard-layout. This part of the proposal
might be a bit more controversial, though; I'm not sure how hard it would
be for implementors.)

*Brian Bi*

Received on 2023-07-01 00:34:35