C++ Logo

std-proposals

Advanced search

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

From: Nikolay Mihaylov <nmmm_at_[hidden]>
Date: Mon, 28 Feb 2022 21:03:48 +0200
no need to deduce the size, I would do it like this:

#include <cstdint>

template<std::size_t IX, typename T>
auto const &get(T const *a){
return a[IX];
}

int main(){
int x[100] = { 0, 1, 2, 3 };

return get<3>(x);
}





On Mon, Feb 28, 2022 at 8:56 PM Giuseppe D'Angelo via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 28/02/2022 17:47, Paolo Di Giglio via Std-Proposals wrote:
> >
> > 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];
> > }
> >
> > As far as I know, it's not possible to pass a C-style array by value to
> > a function nor to return one from a function. So I think there's no need
> > for a r-value reference overload.
>
> Pedantically, there's still the need, as you _can_ form rvalue
> references to arrays (and the array type can be hidden behind a typedef,
> so it's not "obvious").
>
> E.g. a prvalue array can be formed like this:
>
> using A = int[3];
> get<1>(A{1,2,3})
>
> And a xvalue like this:
>
> A a = {1,2,3};
> get<1>(std::move(a));
>
> P2165 is related to this proposal, because by putting get overload in
> namespace std, P2165 would make this work:
>
> int a[] = {1,2,3};
> std::tuple<int, int, int> t = a;
>
>
> My 2 c,
> --
> Giuseppe D'Angelo
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-02-28 19:04:26