Template Specifications for constructor of n-dimensional array
<p>I am implementing an n-dimensional array class which is a template as follows (Note that the data is stored in a linear array whose length is the product of all the dimensions):</p>

<pre><code>template&lt; class valType, int rank &gt;
class NDimensionalArray
{
public:

private:
    valType* m_data;
    int* m_dimensions;
    int m_rank;
};
</code></pre>

<p>So the idea is that a user (me) can specify an array of rank 2, and of a certain dimension, ie:</p>

<pre><code>NDimensionalArray&lt;double,2&gt; matrix(10,10);
</code></pre>

<p>Now the difficulty is in specializing constructors for 1->n dimensions, each constructor takes n parameters where n is the rank of the array. Now I thought of using a valarray like is used in printf(), however with this defining a 1-dimensional array with 2 dimensions ie:</p>

<pre><code>NDimensionalArray&lt;double,1&gt; matrix(10,10);
</code></pre>

<p>would be perfectly acceptable behavior. Is there some neat trick I can use to let the compiler do the repetition? Realistically so long as I know the rank, and have the length of each dimension the constructor can be generic:</p>

<pre><code>{
    int nElements = m_dimensions[0];
    for ( int i=1 ; i&lt;m_rank ; ++i )
        nElements *= m_dimensions[i];
    m_data = new valType[nElements];
}
</code></pre>

<p><strong>Edit:</strong> Note that a similar operation will be needed for accessors.</p>

<p>Also I have considered the option of a constructor which looks like:</p>

<pre><code>NDimensionalArray( const NDimensionalArray&lt;int,1&gt;&amp; dimensions );
</code></pre>

<p>Which could be used like:</p>

<pre><code>NDimensionalArray&lt;int,1&gt; dimVec(2); // Need a specification for 1-dimensional arrays.
dimVec(0) = 10;
dimVec(1) = 10;
NDimensionalArray&lt;double,2&gt; matrix(dimVec);
</code></pre>

<p>This would be a viable solution, but its ugly compared to the use I would like. Also accessing multi-dimensional arrays would become a serious pain, and seriously slow having to construct a dimension vector for each access.</p>
