If needed, here's a version that can deal with a type which can't be default-constructed (try it online):
#include <array>
#include <type_traits>
#include <utility>
#include <tuple>
namespace array_concat_helper {
template <std::size_t Idx, typename SizeSeq, typename Enable=void>
struct indices
{
static constexpr std::size_t tuple_index = 0;
static constexpr std::size_t elem_index = Idx;
};
template <std::size_t Idx, std::size_t FirstN, std::size_t ...Ns>
struct indices<Idx, std::index_sequence<FirstN, Ns...>,
std::enable_if_t<(Idx >= FirstN)>>
{
static constexpr std::size_t tuple_index =
1 + indices<Idx-FirstN, std::index_sequence<Ns...>>::tuple_index;
static constexpr std::size_t elem_index =
indices<Idx-FirstN, std::index_sequence<Ns...>>::elem_index;
};
template <typename T, std::size_t ...Ns, std::size_t ...Is>
std::array<T, (... + Ns)> concat(
std::index_sequence<Is...>,
const std::array<T, Ns>&... arrs)
{
auto arr_tuple = std::tie(arrs...);
return {{
std::get<indices<Is, std::index_sequence<Ns...>>::tuple_index>
(arr_tuple)
[indices<Is, std::index_sequence<Ns...>>::elem_index]...
}};
}
} // end namespace array_concat_helper
template<typename T, std::size_t ...Ns>
std::array<T, (... + Ns)> concat(const std::array<T, Ns>&... arrs)
{
return array_concat_helper::concat(
std::make_index_sequence<(... + Ns)>{}, arrs...);
}
autoas return type?SIZE_L->(... + SIZE_R)(C++17)Tdefault constructible?std::array<NonDefaultConstructible, 2> arr{{ NonDefaultConstructible(42), NonDefaultConstructible(51) }};...