How (if possible) can I use c++11 variadic programming to define a series of vector
's in a function body, (or in other words, a sequence of N
-dimensional arrays with decreasing N
's until 0), like the variables below?
vector<vector<vector<int>>> v<3>;
vector<vector<int>> v<2>;
vector<int> v<1>;
int v<0>;
What I imagined is something like:
#include <iostream>
#include <vector>
using namespace std;
template<int ...> struct seq {};
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };
template<int ...S>
void f(seq<S...>) {
//how do I write definitions of v<N> here?
vector<vector<...(N layers)<vector<int> ...> v<N>; //??how-to, not valid c++
vector<vector<...(N -1 layers)<vector<int> ...> v<N-1>;//??how-to, not valid c++
//...
vector<int> v<1>; //??how-to, not valid c++
int v<0>; //??how-to, not valid c++
//...
}
int main() {
f(typename gens<3>);
return 0;
}
Also, would this be easier in c++14?
Thanks,
--EDIT--
Just to clarify, what I mean by "a tower of vectors" is best described by a N-tuple (v_1, v_2, ..., v_N), where N is an integral template parameter. v_1 is of vector, v_2 is of vector>, and so on.
--EDIT2--
So far, quantdev and R's answers have successfully solved the problem of defining a N-tuple for any fixed N, like 3, but cannot generate a tuple for an unspecified N
. In addition to the functionality in the answers, I need a function which can be used like gen_tower<N>
that returns tuple(v1,v2,...,vN)
.
Consider the example of using variadic programming to compute factorials. I need a function to compute factorial factorial<N>()
for any N
, in addition to the ability to write out any specific expression <1*2*3>
manually. (That was the reason why I asked about variadic programming and whether c++14
will make it easier.)
P.S.
Purely out of personal interests, I want this sequence to hopefully implement a generic function that can read a N-dimensional array from file. I don't know exactly how yet, but I think at step one, I should be able to define the final N
-dimensional array, and the intermediate k
-dimensional arrays for k
from N-1
to 1
. I can read 2-dimensional arrays, and 3-dimensionals. But it would be nice to be able to read arrays of any dimensions.