C++ Logo

std-proposals

Advanced search

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

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Thu, 17 Aug 2023 11:30:08 +0100
On Thu, 2023-08-17 at 09:41 +0100, Gašper Ažman wrote:
> Yes, what Lénárd said, unless you actually need to treat it as a
> std::array, in which case I'm still interested in the answer to my
> question.
>

Pointer interconvertibility still only allows to convert and access
through the pointer if the object of the corresponding type is there.
For some T std::array<T, N> should be implicit-lifetime, so you might
be able to do create the object with start_lifetime_as (or self-
memmove, if applicable). I'm not sure if accessing the elements through
a reference or name to the original C array is defined after this
operation though.

>
> On Thu, Aug 17, 2023, 09:27 Lénárd Szolnoki via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> > On Thu, 2023-08-17 at 09:12 +0100, Frederick Virchanza Gotham via
> > Std-
> > Proposals wrote:
> > > On Wed, Aug 16, 2023 at 11:08 PM Jason McKesson wrote:
> > > > >
> > > > > std::array arr = {
> > > > > something1,
> > > > > something2,
> > > > > something3,
> > > > > };
> > > > >
> > > > > also works.
> > > >
> > > > That deduction guide has been there since C++17, so it's
> > > > standard.
> > > > Assuming of course that all of those are of the same type.
> > >
> > >
> > > I'm looking through cppreference.com here and I don't see a
> > > function
> > > something like:
> > >
> > > template< typename T, std::size_t len >
> > > std::array<T,len> &pretend_is_std_array( T (&arg)[len] ); //
> > > returns
> > > a reference
> > >
> > > What are we supposed to do if we want to treat a C-style array as
> > > though it's an std::array? Should we just reinterpret_cast?
> >
> > No, use std::span. C arrays, std::array and even std::vector and
> > subranges of these are compatible with std::span.
> >
> > Or if you are writing a template then use contiguous_range, if you
> > have
> > to make use of contiguity.
> >
> > Cheers,
> > Lénárd

Received on 2023-08-17 10:30:15