I would like to know if there is a way to transform a std::array into an index sequence?
constexpr std::array<int, 5> x = {0, 3, 4, 5, 8};
constexpr auto integer_sequence = ...; // something that lead to integer_sequence<int, 0, 3, 4, 5, 8>;
I know that it is impossible to use a function, because it will lead to another type. However, since it is possible to do something like :
constexpr std::size_t x = 8;
std::integral_constant<int, x> height;
Is it possible to do it in a simple way?
I tried to do the following :
#include <array>
#include <tuple>
template<std::size_t N>
constexpr std::array<int, N> m_iota(int value) {
std::array<int, N> result{};
for(auto &v : result)
v = value++;
return result;
}
template<int N>
using Int = std::integral_constant<int, N>;
int main() {
constexpr auto array = m_iota<5>(3); // 3, 4, 5, 6, 7
constexpr auto indexer = std::tuple<Int<0>,Int<1>,Int<2>,Int<3>,Int<4>>{};
auto to_sequence = [array](auto ...is) {return std::integer_sequence<int, array[is]...>{};};
auto x = std::apply(to_sequence, indexer);
static_assert(std::is_same_v<decltype(x), std::integer_sequence<int, 3, 4, 5, 6, 7>>);
}
It compiles well on GCC and clang, but not in MSVC... Is there simpler way?
Here a link to msvc : https://godbolt.org/z/EK7azW
invalid template argument for 'std::integer_sequence', expected compile-time constant expressionin theto_sequencelambda. And that makes sense to me.decltype(is)::value? However gcc7.5 does not compile too... It is interesting ahahastd::applydown, it does compile as constant, as it is no longer being used as a callable object. That makes sense to me. But I'm no expert where it comes to compile time programing.std::array<T>can be a template parameter (for suitableT) in C++20.