Note that there are some explanatory texts on larger screens.

plurals
  1. POFill container with template parameters
    primarykey
    data
    text
    <p>I want to fill the template parameters passed to a variadic template into an array with fixed length. For that purpose I wrote the following helper function templates</p> <pre><code>template&lt;typename ForwardIterator, typename T&gt; void fill(ForwardIterator i) { } template&lt;typename ForwardIterator, typename T, T head, T... tail&gt; void fill(ForwardIterator i) { *i = head; fill&lt;ForwardIterator, T, tail...&gt;(++i); } </code></pre> <p>the following class template</p> <pre><code>template&lt;typename T, T... args&gt; struct params_to_array; template&lt;typename T, T last&gt; struct params_to_array&lt;T, last&gt; { static const std::size_t SIZE = 1; typedef std::array&lt;T, SIZE&gt; array_type; static const array_type params; private: void init_params() { array_type result; fill&lt;typename array_type::iterator, T, head, tail...&gt;(result.begin()); return result; } }; template&lt;typename T, T head, T... tail&gt; struct params_to_array&lt;T, head, tail...&gt; { static const std::size_t SIZE = params_to_array&lt;T, tail...&gt;::SIZE + 1; typedef std::array&lt;T, SIZE&gt; array_type; static const array_type params; private: void init_params() { array_type result; fill&lt;typename array_type::iterator, T, last&gt;(result.begin()); return result; } }; </code></pre> <p>and initialized the static constants via</p> <pre><code>template&lt;typename T, T last&gt; const typename param_to_array&lt;T, last&gt;::array_type param_to_array&lt;T, last&gt;::params = param_to_array&lt;T, last&gt;::init_params(); </code></pre> <p>and</p> <pre><code>template&lt;typename T, T head, T... tail&gt; const typename param_to_array&lt;T, head, tail...&gt;::array_type param_to_array&lt;T, head, tail...&gt;::params = param_to_array&lt;T, head, tail...&gt;::init_params(); </code></pre> <p>Now the array</p> <pre><code>param_to_array&lt;int, 1, 3, 4&gt;::params </code></pre> <p>is a <code>std::array&lt;int, 3&gt;</code> and contains the values <code>1</code>, <code>3</code> and <code>4</code>. I think there must be a simpler way to achieve this behavior. Any suggestions?</p> <p><b>Edit:</b> As Noah Roberts suggested in his answer I modified my program like the following: I wrote a new struct counting the elements in a parameter list:</p> <pre><code>template&lt;typename T, T... args&gt; struct count; template&lt;typename T, T head, T... tail&gt; struct count&lt;T, head, tail...&gt; { static const std::size_t value = count&lt;T, tail...&gt;::value + 1; }; template&lt;typename T, T last&gt; stuct count&lt;T, last&gt; { static const std::size_t value = 1; }; </code></pre> <p>and wrote the following function</p> <pre><code>template&lt;typename T, T... args&gt; std::array&lt;T, count&lt;T, args...&gt;::value&gt; params_to_array() { std::array&lt;T, count&lt;T, args...&gt;::value&gt; result; fill&lt;typename std::array&lt;T, count&lt;T, args...&gt;::value&gt;::iterator, T, args...&gt;(result.begin()); return result; } </code></pre> <p>Now I get with</p> <pre><code>params_to_array&lt;int, 10, 20, 30&gt;() </code></pre> <p>a <code>std::array&lt;int, 3&gt;</code> with the content <code>10</code>, <code>20</code> and <code>30</code>. Any further suggestions?</p>
    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.
 

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