C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Only reason I don't use std::array

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 17 Aug 2023 14:15:20 +0300
On Thu, 17 Aug 2023 at 13:50, Timur Doumler via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> std::array provides some guarantees: that the N elements of type T are contiguous, that std::array is an aggregate (with further guarantees on how you can initialise it), that it is a structural type, etc.
>
> AFAIK it does however does *not* provide a guarantee that there is an actual array, that is, a T[N], under the hood anywhere. A hostile implementation could implement std::array<T, 2> for example as a struct { T first, second; };

No, it can't. That implementation wouldn't fulfill the requirements of
array::data(), which are that
[data(), data() + size())
is a valid range (and that range is denoted by pointer arithmetic, and
traversed by pointer arithmetic,
because data() and data()+size() are just pointers).

> In order to make this work, we need a new language facility along the lines of P1912, which is a paper that I unfortunately didn't have time to work on further, but I am hoping to find some time in the future to pick it up again.

We certainly need that paper for various other reasons, but not for a
guarantee that a std::array is backed by an array.

Received on 2023-08-17 11:15:35