I was wondering whether there is something like std::get for accessing any n-dimensional array at compile time. E.g. get_value(arr, 1,2,3) shall return the value arr[1][2][3]. I tried to use a recursive template constexpr. However, the type deduction seems to be problematic.
#include <iostream>
#include <string>
#include <array>
template <class T, class FIRST, class ... REST>
constexpr auto get_value(T arr, FIRST first, REST... rest) {
auto sub = arr[first];
using sub_t = decltype(sub);
return get_value<sub_t, REST...>(sub, rest...);
}
// works for 1D
template<class T>
constexpr auto get_value(T arr, auto first) {
return arr[first];
}
int main()
{
using arr1_t = std::array<int, 5>;
using arr2_t = std::array<arr1_t, 5>;
arr1_t arr1 = {1,2,3,4,5};
arr2_t arr2 = {arr1, arr1, arr1, arr1, arr1};
std::cout << get_value(arr1, 1) << std::endl;
std::cout << get_value(arr2, 1,1) << std::endl;
}
arr[0][1][2]or is it an 1-D array used as multidimensional? - Vittorio Romeo