C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 1 Mar 2022 11:39:51 -0500
On Tue, Mar 1, 2022 at 11:34 AM Barry Revzin <barry.revzin_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.
>

Oh, gross. If it's not going to be SFINAE-friendly, then I don't
particularly have an opinion what it does.

–Arthur

Received on 2022-03-01 16:40:03