Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make generic computations over heterogeneous argument packs of a variadic template function?
    primarykey
    data
    text
    <p><strong>PREMISE:</strong></p> <p>After playing around with variadic templates a little bit, I realized that achieving anything which goes slightly beyond the trivial meta-programming tasks soon becomes pretty cumbersome. In particular, I found myself wishing for a way to perform <strong>generic operations over an argument pack</strong> such as <em>iterate</em>, <em>split</em>, <em>loop</em> in a <code>std::for_each</code>-like fashion, and so on.</p> <p>After watching <a href="http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Static-If-I-Had-a-Hammer%20I%20have" rel="nofollow noreferrer">this lecture by Andrei Alexandrescu</a> from C++ and Beyond 2012 on the desirability of <code>static if</code> into C++ (a construct borrowed from the <a href="http://dlang.org/index.html" rel="nofollow noreferrer">D Programming Language</a>) I had the feeling that some sort of <code>static for</code> would come handy as well - and I feel more of these <code>static</code> constructs could bring benefit.</p> <p>So I started wondering if there is a way to achieve <strong>something like this</strong> for argument packs of a variadic template function (<em>pseudo-code</em>):</p> <pre><code>template&lt;typename... Ts&gt; void my_function(Ts&amp;&amp;... args) { static for (int i = 0; i &lt; sizeof...(args); i++) // PSEUDO-CODE! { foo(nth_value_of&lt;i&gt;(args)); } } </code></pre> <p>Which would get translated <em>at compile-time</em> into something like this:</p> <pre><code>template&lt;typename... Ts&gt; void my_function(Ts&amp;&amp;... args) { foo(nth_value_of&lt;0&gt;(args)); foo(nth_value_of&lt;1&gt;(args)); // ... foo(nth_value_of&lt;sizeof...(args) - 1&gt;(args)); } </code></pre> <p>In principle, <code>static_for</code> would allow for even more elaborate processing:</p> <pre><code>template&lt;typename... Ts&gt; void foo(Ts&amp;&amp;... args) { constexpr s = sizeof...(args); static for (int i = 0; i &lt; s / 2; i++) { // Do something foo(nth_value_of&lt;i&gt;(args)); } static for (int i = s / 2; i &lt; s; i++) { // Do something different bar(nth_value_of&lt;i&gt;(args)); } } </code></pre> <p>Or for a more expressive idiom like this one:</p> <pre><code>template&lt;typename... Ts&gt; void foo(Ts&amp;&amp;... args) { static for_each (auto&amp;&amp; x : args) { foo(x); } } </code></pre> <p><strong>RELATED WORK:</strong></p> <p>I did some search on the Web and found out that <em>something</em> does indeed exist:</p> <ul> <li><a href="http://blog.shandyba.com/2009/12/17/converting-variadic-template-arguments-pack-to-boost-mpl-sequence/" rel="nofollow noreferrer">This link</a> describes how to convert a parameter pack into a Boost.MPL vector, but that only goes half the way (if not less) towards the goal;</li> <li><a href="https://stackoverflow.com/questions/5484930/split-variadic-template-arguments">this question on SO</a> seems to call for a similar and slightly related meta-programming feature (splitting an argument pack into two halves) - actually, there are several questions on SO which seem to be related to this issue, but none of the answer I have read solves it satisfactorily IMHO;</li> <li><a href="http://www.boost.org/libs/fusion/" rel="nofollow noreferrer">Boost.Fusion</a> defines algorithms for converting an argument pack into a <em>tuple</em>, but I would prefer: <ol> <li>not to create <strong>unnecessary temporaries</strong> to hold arguments that can (and should be) perfectly forwarded to some generic algorithms;</li> <li>have a <strong>small, self-contained</strong> library to do that, while Boost.Fusion is likely to include way more stuff than is needed to address this issue.</li> </ol></li> </ul> <p><strong>QUESTION:</strong></p> <p>Is there a relatively simple way, possibly through some template meta-programming, to achieve what I am looking for without incurring in the limitations of the existing approaches?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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