Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's fairly simple to do with parameter pack expansion, even if you can't rewrite <code>f</code> to return the output parameter by value:</p> <pre><code>struct pass { template&lt;typename ...T&gt; pass(T...) {} }; template&lt;typename... Args&gt; void h(Args... args) { const size_t nargs = sizeof...(args); // get number of args X x_array[nargs]; // create X array of that size X *x = x_array; int unused[]{(f(*x++, args), 1)...}; // call f pass{unused}; g(x_array, nargs); // call g with x_array } </code></pre> <p>It should be possible just to write</p> <pre><code> pass{(f(*x++, args), 1)...}; // call f </code></pre> <p>but it appears g++ (4.7.1 at least) has a bug where it fails to order the evaluation of brace-initializer-list parameters as class initialisers. Array initialisers are OK though; see <a href="https://stackoverflow.com/questions/10764867/sequencing-among-a-variadic-expansion">Sequencing among a variadic expansion</a> for more information and examples.</p> <p><a href="http://liveworkspace.org/code/f88c255cbfd03830476f31768eca8183" rel="nofollow noreferrer">Live example</a>.</p> <hr> <p>As an alternative, here's the technique mentioned by Xeo using a generated index pack; unfortunately it does require an extra function call and parameter, but it is reasonably elegant (especially if you happen to have an index pack generator lying around):</p> <pre><code>template&lt;int... I&gt; struct index { template&lt;int n&gt; using append = index&lt;I..., n&gt;; }; template&lt;int N&gt; struct make_index { typedef typename make_index&lt;N - 1&gt;::type::template append&lt;N - 1&gt; type; }; template&lt;&gt; struct make_index&lt;0&gt; { typedef index&lt;&gt; type; }; template&lt;int N&gt; using indexer = typename make_index&lt;N&gt;::type; template&lt;typename... Args, int... i&gt; void h2(index&lt;i...&gt;, Args... args) { const size_t nargs = sizeof...(args); // get number of args X x_array[nargs]; // create X array of that size pass{(f(x_array[i], args), 1)...}; // call f g(x_array, nargs); // call g with x_array } template&lt;typename... Args&gt; void h(Args... args) { h2(indexer&lt;sizeof...(args)&gt;(), std::forward&lt;Args&gt;(args)...); } </code></pre> <p>See <a href="https://stackoverflow.com/questions/10766112/c11-i-can-go-from-multiple-args-to-tuple-but-can-i-go-from-tuple-to-multiple">C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args?</a> for more information. <a href="http://liveworkspace.org/code/44e8ec1545dcbdad55d448c603195c3d" rel="nofollow noreferrer">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. 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.
 

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