Quick A: Use integer_sequence with a helper function.
Recently on SO:
Array Initialisation Compile Time - Constexpr Sequence
1) How to implement that kind of integer_sequence?
template <std::size_t... Is> constexpr auto make_sequence_impl(std::index_sequence<Is...>) { return std::index_sequence<generate_ith_number(Is)...>{}; } template <std::size_t N> constexpr auto make_sequence() { return make_sequence_impl(std::make_index_sequence<N>{}); }2) Is it possible to build an std::array from that integer_sequence at compile time?
template <std::size_t... Is> constexpr auto make_array_from_sequence_impl(std::index_sequence<Is...>) { return std::array<std::size_t, sizeof...(Is)>{Is...}; } template <typename Seq> constexpr auto make_array_from_sequence(Seq) { return make_array_from_sequence_impl(Seq{}); }Usage:
int main() { constexpr auto arr = make_array_from_sequence(make_sequence<6>()); static_assert(arr[0] == 0); static_assert(arr[1] == 1); static_assert(arr[2] == 2); static_assert(arr[3] == 4); static_assert(arr[4] == 5); static_assert(arr[5] == 7); }
Add a Comment
Comments are closed.