Note that there are some explanatory texts on larger screens.

plurals
  1. POusing nested std::array to create an multidimensional array without knowing dimensions or extents until runtime
    primarykey
    data
    text
    <p>Is it possible for a class to have a member which is a multidimensional array whose dimensions and extents are not known until runtime?</p> <p>I have found (via <a href="http://cpptruths.blogspot.com/2011/10/multi-dimensional-arrays-in-c11.html" rel="nofollow noreferrer" title="guide">this guide</a>) a way to create a struct to easily nest std::arrays at compile time using template metaprogramming:</p> <pre><code>#include &lt;array&gt; /* this struct allows for the creation of an n-dimensional array type */ template &lt;typename T,size_t CurrentDimExtent,size_t... NextDimExtent&gt; struct MultiDimArray{ public: //define the type name nestedType to be a recursive template definition. using nestedType=typename MultiDimArray&lt;T,NextDimExtent...&gt;::type; using type=std::array&lt;nestedType,CurrentDimExtent&gt;; }; /* This struct is the template specialization which handles the base case of the final dimensional extent */ template &lt;typename T,size_t DimExtent&gt; struct MultiDimArray&lt;T,DimExtent&gt;{ using type=std::array&lt;T,DimExtent&gt;; }; </code></pre> <p>this still falls short of satisfying my requirement in two ways (that I know of):</p> <ol> <li>In order to declare a variable (or a pointer to a variable) of this type you must state the dimensions.</li> <li>This only works when the DimExtents are constant expressions (set at compile time). </li> </ol> <p>To demonstrate why number 2 is a distinct problem, here is a class with a set number of dimensions (2) using a void* to reference the multidimensional array:</p> <pre><code>template &lt;typename T&gt; class TwoDimGrid{ public: TwoDimGrid(const size_t extent1,const size_t extent2): _twoDimArray(new MultiDimArray&lt;T,extent1,extent2&gt;); private: void* _twoDimArray; }; </code></pre> <p>This will not compile as extent1 and extent2 are not constant expressions.</p> <p>other notes:</p> <ul> <li>I would like to see if it's possible to accomplish using std:array, rather than native arrays or a dynamically resizing container like std::vector.</li> <li>Please use smart pointers where appropriate (I didn't as I'm not really sure how to handle a smart void pointer).</li> </ul> <h1>Edit</h1> <p>I have fallen into the trap of <a href="https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem">The XY Problem</a> with X being the first sentence of this question and Y being how to accomplish it with std::array. I therefore created a <a href="https://stackoverflow.com/questions/20226270/is-it-possible-in-c-for-a-class-to-have-a-member-which-is-a-multidimensional-a">new question</a> and am leaving this one here in case it's ever possible to solve Y problem.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload