Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Proof Of Concept</h2> <p>Ok, inspired by UncleBens challenge <a href="https://stackoverflow.com/questions/7135093/array-with-undefined-size-as-class-member/7135200#7135200">here</a>, I came up with a Proof-Of-Concept (<em>see below</em>) that let's you actually do:</p> <blockquote> <pre><code> srand(123); for (int i=0; i&lt;10; i++) { size_t N = rand() % DEMO_MAX; // capped for demo purposes std::auto_ptr&lt;iarray&gt; dyn(make_dynamic_array(N)); exercise(*dyn); } </code></pre> </blockquote> <p>It revolves around a template trick in <code>factory&lt;&gt;::instantiate</code> that actually uses a compile-time meta-binary-search to match the specified (runtime) dimension to a range of explicit <code>static_array</code> class template instantiations. </p> <p>I feel the need to repeat that this is not good design, I provide the code sample only to show what the limits are of what can be done - with reasonable effor, to achieve the actual goal of the question. You can see the drawbacks:</p> <ul> <li>the compiler is crippled with a boatload of useless statical types and create classes that are so big that they become a performance liability or a reliability hazard (stack allocation anyone? -> we're on <em>'stack overflow'</em> already :))</li> <li>at <code>DEMO_MAX = 256</code>, <code>g++ -Os</code> will actually emit 258 instantiations of <code>factory&lt;&gt;</code>; <code>g++ -O4</code> will keep 74 of those, inlining the rest[2]</li> <li>compilation doesn't scale well: at <code>DEMO_MAX = MAX_RAND</code> compilation takes about 2m9s to... run out of memory on a 64-bit 8GB machine; at <code>MAX_RAND&gt;&gt;16</code> it takes over 25 minutes to <em>possibly</em> compile (?) while nearly running out of memory. It would really require some amounts of ugly manual optimization to remove these limits - I haven't gone so insane as to actually do that work, if you'll excuse me.</li> <li>on the upside, this sample demonstrates the arguably sane range for this class (0..256) and compiles in only 4 seconds and 800Kb on my 64-bit linux. See also a <strong><a href="http://codepad.org/r4RDwUKG" rel="nofollow noreferrer">down-scaled, ANSI-proof version at codepad.org</a></strong></li> </ul> <p>[2] <sup>established that with <code>objdump -Ct test | grep instantiate | cut -c62- | sort -k1.10n</code></sup></p> <h3>Show me the <em>CODE</em> already!</h3> <pre><code>#include &lt;iostream&gt; #include &lt;memory&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; #include &lt;stdexcept&gt; struct iarray { typedef int value_type; typedef value_type* iterator; typedef value_type const* const_iterator; typedef value_type&amp; reference; typedef value_type const&amp; const_reference; virtual size_t size() const = 0; virtual iterator begin() = 0; virtual const_iterator begin() const = 0; // completely unoptimized plumbing just for demonstration purps here inline iterator end() { return begin()+size(); } inline const_iterator end() const { return begin()+size(); } // boundary checking would be 'gratis' here... for compile-time constant values of 'index' inline const_reference operator[](size_t index) const { return *(begin()+index); } inline reference operator[](size_t index) { return *(begin()+index); } // virtual ~iarray() {} }; template &lt;size_t N&gt; struct static_array : iarray { static const size_t _size = N; value_type data[N]; virtual size_t size() const { return _size; } virtual iterator begin() { return data; } virtual const_iterator begin() const { return data; } }; #define DEMO_MAX 256 template &lt;size_t PIVOT=DEMO_MAX/2, size_t MIN=0, size_t MAX=DEMO_MAX&gt; struct factory /* this does a binary search in a range of static types * * due to the binary search, this will require at most 2log(MAX) levels of * recursions. * * If the parameter (size_t n) is a compile time constant expression, * together with automatic inlining, the compiler will be able to optimize * this all the way to simply returning * * new static_array&lt;n&gt;() * * TODO static assert MIN&lt;=PIVOT&lt;=MAX */ { inline static iarray* instantiate(size_t n) { if (n&gt;MAX || n&lt;MIN) throw std::range_error("unsupported size"); if (n==PIVOT) return new static_array&lt;PIVOT&gt;(); if (n&gt;PIVOT) return factory&lt;(PIVOT + (MAX-PIVOT+1)/2), PIVOT+1, MAX&gt;::instantiate(n); else return factory&lt;(PIVOT - (PIVOT-MIN+1)/2), MIN, PIVOT-1&gt;::instantiate(n); } }; iarray* make_dynamic_array(size_t n) { return factory&lt;&gt;::instantiate(n); } void exercise(iarray&amp; arr) { int gen = 0; for (iarray::iterator it=arr.begin(); it!=arr.end(); ++it) *it = (gen+=arr.size()); std::cout &lt;&lt; "size " &lt;&lt; arr.size() &lt;&lt; ":\t"; std::copy(arr.begin(), arr.end(), std::ostream_iterator&lt;int&gt;(std::cout, ",")); std::cout &lt;&lt; std::endl; } int main() { { // boring, oldfashioned method static_array&lt;5&gt; i5; static_array&lt;17&gt; i17; exercise(i5); exercise(i17); } { // exciting, newfangled, useless method for (int n=0; n&lt;=DEMO_MAX; ++n) { std::auto_ptr&lt;iarray&gt; dyn(make_dynamic_array(n)); exercise(*dyn); } try { make_dynamic_array(-1); } catch (std::range_error e) { std::cout &lt;&lt; "range error OK" &lt;&lt; std::endl; } try { make_dynamic_array(DEMO_MAX + 1); } catch (std::range_error e) { std::cout &lt;&lt; "range error OK" &lt;&lt; std::endl; } return 0; srand(123); for (int i=0; i&lt;10; i++) { size_t N = rand() % DEMO_MAX; // capped for demo purposes std::auto_ptr&lt;iarray&gt; dyn(make_dynamic_array(N)); exercise(*dyn); } } return 0; } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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