Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We still lack a lot of helpers to manipulate variadic parameter packs (or I am not aware of them). Until a nice Boost library bring them to us, we can still write our own.</p> <p>For example, if you are willing to postpone your arrays initialization to the constructor body, you can create and use a fonction that copies part of the parameter pack to an output iterator:</p> <pre><code>#include &lt;array&gt; #include &lt;cassert&gt; #include &lt;iostream&gt; // Copy n values from the parameter pack to an output iterator template &lt; typename OutputIterator &gt; void copy_n( size_t n, OutputIterator ) { assert ( n == 0 ); } template &lt; typename OutputIterator, typename T, typename... Args &gt; void copy_n( size_t n, OutputIterator out, const T &amp; value, Args... args ) { if ( n &gt; 0 ) { *out = value; copy_n( n - 1, ++out, args... ); } } // Copy n values from the parameter pack to an output iterator, starting at // the "beginth" element template &lt; typename OutputIterator &gt; void copy_range( size_t begin, size_t size, OutputIterator out ) { assert( size == 0 ); } template &lt; typename OutputIterator, typename T, typename... Args &gt; void copy_range( size_t begin, size_t size, OutputIterator out, T value, Args... args ) { if ( begin == 0 ) { copy_n( size, out, value, args... ); } else { copy_range( begin - 1, size, out, args... ); } } template &lt; int N &gt; struct DoubleArray { std::array&lt; int, N &gt; p; std::array&lt; int, N &gt; q; template &lt; typename... Args &gt; DoubleArray ( Args... args ) { copy_range( 0, N, p.begin(), args... ); copy_range( N, N, q.begin(), args... ); } }; int main() { DoubleArray&lt;3&gt; mya(1, 2, 3, 4, 5, 6); std::cout &lt;&lt; mya.p[0] &lt;&lt; mya.p[2] &lt;&lt; std::endl; std::cout &lt;&lt; mya.q[0] &lt;&lt; mya.q[2] &lt;&lt; std::endl; } // Ouput: // 13 // 46 </code></pre> <p>As you can see, you can (not so) easily create your own algorithms to manipulate parameter packs; all is needed is a good understanding of recursion and pattern matching (as always when doing Template MetaProgramming...).</p>
 

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