I was writting some class that map specific multidimensionnal array to unidimensionnal array (like 2D array N by M size is like a 1D array NM size you know, and then you can accees the cell [n, m] through [n+mN]). And it was quite boring since I must handle any multidimensional array (forced to copy/paste many times the class definition).
And I found something that could be great in my case: variadic template function. I would like to have my constructor and accessors using variadic template so my accessors can use any number of parameters (2 for 2D array, 3 for 3D array...) and same for my constructor, since I need to save the size in each dimension (N, M, ...) and multiply then to have the size of my unidimentionnal array.
The problem is that I don't know how to do that :(
Every examples I found relies on two functions, one with one parameter of type T
and another one with one parameter of type T
and the Args... args
).
Is it possible in one function ? Without recursion or so ? I gave you what I've made so far:
template <typename T, typename... Args>
T returner(T v, Args... args){
return v;
}
template <typename T>
class Array{
public:
int* dim; //contain the size of each dimension
T* array; //the actual array
template<typename... Args>
Array(Args... args){
constexpr int size = sizeof...(Args);
dim = new int[size];
for(int i=0; i<size; i++)
dim[i] = returner(args...);
/*dim[0] should be equal to the first argument, dim[1]
should be equal to the second argument and so on */
}
};
int main(){
Array<int>(2,2,2); // meant to be a 3D array, 2cells in each dimension
return 0:
}
Obviously, "returner" always return the first argument and I understand why, but the only solution I see it to pass the dim
array as a parameter and I would like to not do that. Is there a solution ??
PS: I could do that with classical variadic function, like in C, but it would be quite bad in performance :(