title: Recursive indexing for tensor in C++11
body: <p>I have a tensor classes of rank N which wrap data stored in an array. For example, a rank-3 tensor would have dimensions (d0,d1,d2) and a unique element would be accessed with the multi-index (i0,i1,i2) from the underlying array of length d0*d1*d2. If d0=d1=d2=10, i0=1, i1=2, i2=3, then element 123 of the array would be accessed. </p>
  
  <p>I've implemented a recursively defined class which computes single array index from the multi-index as follows:</p>
  
  <pre><code>template&lt;size_t N&gt;
  class TensorIndex : TensorIndex&lt;N-1&gt; {
  private:
    size_t d;
  public:
  template&lt;typename...Ds&gt;
  TensorIndex( size_t d0, Ds...ds ) : TensorIndex&lt;N-1&gt;( ds... ), d(d0) {}
    template&lt;typename...Is&gt;
    size_t index( size_t i0, Is...is ) {
      return i0+d*TensorIndex&lt;N-1&gt;::index(is...);
    }
  };
  
  template&lt;&gt;
  struct TensorIndex&lt;1&gt; {
  TensorIndex( size_t ) {}
    size_t index( size_t i ) { return i; }
  };
  </code></pre>
  
  <p>Which reverses the desired order.</p>
  
  <pre><code>TensorIndex&lt;3&gt; g(10,10,10);
  std::cout &lt;&lt; g.index(1,2,3) &lt;&lt; std::endl;
  </code></pre>
  
  <p>outputs 321. What would be a simple way to reverse the order of the arguments for the constructor and index functions?</p>
  
  <p>Edit:
  I tried implementing using the suggested approach of reversing the variadic arguments, but this was suboptimal as it required reversing the arguments for both index and the constructor and the necessary helper functions for these two cases would appear slightly different. The initializer list answer looks more straightforward. </p>