C++ Logo

std-proposals

Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 28 Feb 2022 19:56:05 +0100
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

Received on 2022-02-28 18:56:10