When I call: a7[0][1][100];
I am able to obtain the first index 0
in the operator[]
but as index I won't able to obtain other index values 1 and 100 as recursively. How could I able to use operator[]
in order to obtain recursive following index values. In this example for the 3 dimensional array, operator[]
is only called only once for the first dimension which is 0
.
My Example Code is as follows:
template <class T, unsigned ... RestD> struct array;
template <class T, unsigned PrimaryD>
struct array <T, PrimaryD> {
typedef T type[PrimaryD];
type data;
T& operator[] (unsigned i) {
return data[i];
}
};
template <class T, unsigned PrimaryD, unsigned ... RestD>
struct array <T, PrimaryD, RestD...> {
typedef typename array<T, RestD...>::type OneDimensionDownArrayT;
typedef OneDimensionDownArrayT type[PrimaryD];
type data;
OneDimensionDownArrayT& operator[] (int i) {
OneDimensionDownArrayT& a = data[i];
return a;
}
};
int main () {
array<int, 1, 2, 3> a7 {{{{1, 2, 3},{4, 5, 6}}}};
a7[0][1][2] = 100; //=>won't recursively go through operator[]
//I want to recursively obtain 0, 1 and 2 as index values
a7[0][1][100] = 100; //also works correctly.
std::cout << a7[0][1][100] << std::endl;
return 0;
}