c++ - Arbitrary dimensional array using Variadic templates -
how can create array class in c++11 can used
array < int, 2, 3, 4> a, b; array < char, 3, 4> d; array < short, 2> e;
and access in way like
a[2][1][2] = 15; d[1][2] ='a';
i need overload operator as
t &operator[size_t i_1][size_t i_2]...[size_t i_d];
which not exist. how can this?
the simplest way nesting std::array
:
#include<array> template<class t, size_t size, size_t... sizes> struct arrayimpl { using type = std::array<typename arrayimpl<t, sizes...>::type, size>; }; template<class t, size_t size> struct arrayimpl<t, size> { using type = std::array<t, size>; }; template<class t, size_t... sizes> using array = typename arrayimpl<t, sizes...>::type;
in solution array<char, 3, 4>
same std::array<std::array<char, 4>, 3>
- array consisting of arrays of smaller dimension.
this shows how can implement operator[]
many dimensions. operator[]
of object needs return object operator[]
defined. in case reference array of smaller dimension.
Comments
Post a Comment