Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could refactor or wrap <code>f</code> to return a new <code>X</code> instead of having it passed, since this would play pack expansion into the hand and make the function really concise:</p> <pre><code>template&lt;class T&gt; X fw(T const&amp; t){ X x; f(x, t); return x; } template&lt;class... Args&gt; void h(Args... args){ X xs[] = { fw(args)... }; g(xs, sizeof...(Args)); } </code></pre> <p><a href="http://liveworkspace.org/code/2667351a3a6fd53c54c1ba1abf6d8466">Live example.</a></p> <p>And if you could change <code>g</code> to just accept an <code>std::initializer_list</code>, it would get even more concise:</p> <pre><code>template&lt;class... Args&gt; void h(Args... args){ g({f(args)...}); } </code></pre> <p><a href="http://liveworkspace.org/code/363aeabcede4f39ac3bf9c14c71cc46a">Live example.</a> Or (maybe better), you could also provide just a wrapper <code>g</code> that forwards to the real <code>g</code>:</p> <pre><code>void g(X const*, unsigned){} void g(std::initializer_list&lt;X&gt; const&amp; xs){ g(xs.begin(), xs.size()); } template&lt;class... Args&gt; void h(Args... args){ g({f(args)...}); } </code></pre> <p><a href="http://liveworkspace.org/code/43b68fff53eac856768e60e855f05465">Live example.</a><br> <strong>Edit:</strong> Another option is using a temporary array:</p> <pre><code>template&lt;class T&gt; using Alias = T; template&lt;class T&gt; T&amp; as_lvalue(T&amp;&amp; v){ return v; } template&lt;class... Args&gt; void h(Args... args){ g(as_lvalue(Alias&lt;X[]&gt;{f(args)...}), sizeof...(Args)); } </code></pre> <p><a href="http://liveworkspace.org/code/6667474973ad0dba0fa8c0124f86d83c">Live example.</a> Note that the <code>as_lvalue</code> function is dangerous, the array still only lives until the end of the full expression (in this case <code>g</code>), so be cautious when using it. The <code>Alias</code> is needed since just <code>X[]{ ... }</code> is not allowed due to the language grammar.</p> <p>If all of that's not possible, you'll need recursion to access all elements of the <code>args</code> pack.</p> <pre><code>#include &lt;tuple&gt; template&lt;unsigned&gt; struct uint_{}; // compile-time integer for "iteration" template&lt;unsigned N, class Tuple&gt; void h_helper(X (&amp;)[N], Tuple const&amp;, uint_&lt;N&gt;){} template&lt;unsigned N, class Tuple, unsigned I = 0&gt; void h_helper(X (&amp;xs)[N], Tuple const&amp; args, uint_&lt;I&gt; = {}){ f(xs[I], std::get&lt;I&gt;(args)); h_helper(xs, args, uint_&lt;I+1&gt;()); } template&lt;typename... Args&gt; void h(Args... args) { static constexpr unsigned nargs = sizeof...(Args); X xs[nargs]; h_helper(xs, std::tie(args...)); g(xs, nargs); } </code></pre> <p><a href="http://liveworkspace.org/code/97da6870842756030cb7815367297e33">Live example.</a></p> <p><strong>Edit:</strong> Inspired by ecatmur's comment, I employed the <a href="http://loungecpp.wikidot.com/tips-and-tricks%3aindices">indices trick</a> to make it work with just pack expansion and with <code>f</code> and <code>g</code> as-is, without altering them.</p> <pre><code>template&lt;unsigned... Indices&gt; struct indices{ using next = indices&lt;Indices..., sizeof...(Indices)&gt;; }; template&lt;unsigned N&gt; struct build_indices{ using type = typename build_indices&lt;N-1&gt;::type::next; }; template &lt;&gt; struct build_indices&lt;0&gt;{ using type = indices&lt;&gt;; }; template&lt;unsigned N&gt; using IndicesFor = typename build_indices&lt;N&gt;::type; template&lt;unsigned N, unsigned... Is, class... Args&gt; void f_them_all(X (&amp;xs)[N], indices&lt;Is...&gt;, Args... args){ int unused[] = {(f(xs[Is], args), 1)...}; (void)unused; } template&lt;class... Args&gt; void h(Args... args){ static constexpr unsigned nargs = sizeof...(Args); X xs[nargs]; f_them_all(xs, IndicesFor&lt;nargs&gt;(), args...); g(xs, nargs); } </code></pre> <p><a href="http://liveworkspace.org/code/cbeb0698d38787368557e7b60e283220">Live example.</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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