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 09:27:39 +0100
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 08:27:43