On Sat, 30 Nov 2024 at 17:30, Jonathan Wakely <cxx@kayari.org> wrote:


On Sat, 30 Nov 2024, 14:16 Desmond Gold via Std-Proposals, <std-proposals@lists.isocpp.org> wrote:


---------- Forwarded message ---------
From: Desmond Gold <desmondbongcawel922@gmail.com>
Date: Sat, Nov 30, 2024 at 8:15 PM
Subject: destructurable std::integer_sequence
To: <std-proposals@lists.isocpp.org>


After  P1061R10 has been approved for C++26, it's now time to let std::integer_sequence be a destructurable type for improved usability while still remain its niche

auto [...Is] = std::make_index_sequence<N>();

This addition helps simplify metaprogramming implementations that rely on std::integer_sequence. Most of these implementations declare another helper function that takes std::integer_sequence that does the actual work. But with this addition, you don't need to do that.

// before:
template <typename Array, std::size_t... I>
constexpr auto array_to_tuple_impl(const Array& a, std::index_sequence<I...>)
{
    return std::make_tuple(a[I]...);
}

template <typename T, std::size_t N, typename Indx = std::make_index_sequence<N>>
constexpr auto array_to_tuple(const std::array<T, N>& a)
{
    return details::array_to_tuple_impl(a, Indx{});
}

// after:
template <typename T, std::size_t N>
constexpr auto array_to_tuple(const std::array<T, N>& a)
{
    auto [...Is] = std::make_index_sequence<N>();
    return std::make_tuple(a[Is]...);
}

Can't you just do: 

auto& [e...] = a;
return std::tuple(e...);

?

Actually you don't even need that, because std::array is tuple-like so you can just say std::tuple(a) and it works.

But let's say we have some  structural type that isn't tuple-like, then you can just unpack it into a pack, and then expand that. You don't need an index_sequence.

auto [e...] = a; // unpack a into e...
return SomethingElse(e...);