A Question About Variadic Templates

hello! I was recently introduced to variadic templates in C++ by a friend and i feel like i have an use case that would really benefit from them but i can't really wrap my head around them and the syntax of the ellipsis (...) operator

I have reduced my code to this short snippet, it is for a linear algebra library i am developing for personal use:

template<int first>
class Tensor{
float data[first];
};

template<int first, int... sizes>
class Tensor{
Tensor<sizes...> data[first];
};

as i understand it, this should define a template where the amount of template arguments that are passed should determine the "depth" of the data structure; each value determines the amount of elements stored on each "node" of the data structure.

for example:

Tensor<3,2,4> myTensor;

should essentially create a three dimensional array of float of size 3x2x4

can i get some help pointing out where i am wrong and maybe a piece of code that reflects the right way to do what i am pursuing?
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template < typename T, std::size_t... > struct tensor ; // base template (not defined)

// specialisation for one dimension
template < typename T, std::size_t N > struct tensor<T,N>
{
    using array_type = T[N] ;
    array_type data {} ;
};

// specialisation for more than one dimension
template < typename T, std::size_t FIRST, std::size_t... REST >
struct tensor<T,FIRST,REST...>
{
    using inner_array_type = typename tensor<T,REST...>::array_type ;
    using array_type = inner_array_type[FIRST] ;
    array_type data {} ;
};

http://coliru.stacked-crooked.com/a/0b01a932d0a1cfca
Topic archived. No new replies allowed.