C++ Logo

std-proposals

Advanced search

Re: [std-proposals] C-style array specialization for std::get

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Tue, 1 Mar 2022 17:42:59 +0000
Hi,

On Tue, 1 Mar 2022 10:33:53 -0600
Barry Revzin via Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Tue, Mar 1, 2022 at 9:59 AM Arthur O'Dwyer via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
> > On Tue, Mar 1, 2022 at 10:13 AM Nikolay Mihaylov via Std-Proposals <
> > std-proposals_at_[hidden]> wrote:
> >
> >> Once again, why do we need the size?
> >> Shall we check the size?
> >> Via static_assert?
> >> During runtime?
> >>
> >
> > In the proposed std::get for C arrays, AIUI, the size would be part
> > of the signature. It would look like this:
> >
> > template<size_t X, class T, size_t N> requires (X < N)
> > constexpr T& get(T (&arr)[N]) noexcept {
> > return arr[X];
> > }
> >
>
> No, it wouldn't. Paulo's email contained the correct implementation:
>
> template <std::size_t Idx, typename T, std::size_t N>
> constexpr T& get(T (&arr)[N]) noexcept
> {
> static_assert(Idx < N, "Index out of bounds");
> return arr[Idx];
> }
>
> This would match what std::get does for std::array, std::pair, and
> std::tuple.

It still needs an rvalue overload, the others have it.

>
> Barry

Cheers,
Lénárd Szolnoki

Received on 2022-03-01 17:43:05