(Spoiler - this is a self-answered question)
Let's pretend I have two index sequences, for example using i1 = std::index_sequence<1, 3, 5, 7>;
and using i2 = std::index_sequence<2, 4, 6, 8>;
I want to make an array (at compile time) which would have 8 elements in it in sequence: 1, 2, 3, 4, 5, 6, 7, 8
, so that following code would work (say, at global scope):
std::array<int, 8> arr = make_array(i1{}, i2{});
Note: if I just want one sequence, the solution is straightforward:
template<size_t... Ix>
constexpr auto make_arr(std::index_sequence<Ix...> )
return std::array{Ix...};
}
But it is not that trivial if I need to join two sequences, for example, this doesn't work:
template<size_t... Ix1, size_t... Ix2>
constexpr auto make_arr(std::index_sequence<Ix1...>, std::index_sequence<Ix2...>)
return std::array{(Ix1, Ix2)...};
}
(Above code will just populate array with values from second sequence).
Another potential solution would be to use constexpr
function which would first define an array with default values, and than copy values from index sequences into it, but while that work with ints, that wouldn't work with some more elaborate types, which are not default-constructible (obviously, they wouldn't be part of index sequences, but they could be something else).
Is there any solution which would not require looping and default-constructing values? Any available C++ standard is fair game.
1 3 5 7 2 4 6 8
an acceptable result? – Absorptivity