Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In C++ there is many ways of expanding/unpacking tuple and apply those tuple elements to a variadic template function. Here is a small helper class which creates index array. It is used a lot in template metaprogramming:</p> <pre><code>// ------------- UTILITY--------------- template&lt;int...&gt; struct index_tuple{}; template&lt;int I, typename IndexTuple, typename... Types&gt; struct make_indexes_impl; template&lt;int I, int... Indexes, typename T, typename ... Types&gt; struct make_indexes_impl&lt;I, index_tuple&lt;Indexes...&gt;, T, Types...&gt; { typedef typename make_indexes_impl&lt;I + 1, index_tuple&lt;Indexes..., I&gt;, Types...&gt;::type type; }; template&lt;int I, int... Indexes&gt; struct make_indexes_impl&lt;I, index_tuple&lt;Indexes...&gt; &gt; { typedef index_tuple&lt;Indexes...&gt; type; }; template&lt;typename ... Types&gt; struct make_indexes : make_indexes_impl&lt;0, index_tuple&lt;&gt;, Types...&gt; {}; </code></pre> <p>Now the code which does the job is not that big:</p> <pre><code> // ----------UNPACK TUPLE AND APPLY TO FUNCTION --------- #include &lt;tuple&gt; #include &lt;iostream&gt; using namespace std; template&lt;class Ret, class... Args, int... Indexes &gt; Ret apply_helper( Ret (*pf)(Args...), index_tuple&lt; Indexes... &gt;, tuple&lt;Args...&gt;&amp;&amp; tup) { return pf( forward&lt;Args&gt;( get&lt;Indexes&gt;(tup))... ); } template&lt;class Ret, class ... Args&gt; Ret apply(Ret (*pf)(Args...), const tuple&lt;Args...&gt;&amp; tup) { return apply_helper(pf, typename make_indexes&lt;Args...&gt;::type(), tuple&lt;Args...&gt;(tup)); } template&lt;class Ret, class ... Args&gt; Ret apply(Ret (*pf)(Args...), tuple&lt;Args...&gt;&amp;&amp; tup) { return apply_helper(pf, typename make_indexes&lt;Args...&gt;::type(), forward&lt;tuple&lt;Args...&gt;&gt;(tup)); } </code></pre> <p>Test is shown bellow:</p> <pre><code>// --------------------- TEST ------------------ void one(int i, double d) { std::cout &lt;&lt; "function one(" &lt;&lt; i &lt;&lt; ", " &lt;&lt; d &lt;&lt; ");\n"; } int two(int i) { std::cout &lt;&lt; "function two(" &lt;&lt; i &lt;&lt; ");\n"; return i; } int main() { std::tuple&lt;int, double&gt; tup(23, 4.5); apply(one, tup); int d = apply(two, std::make_tuple(2)); return 0; } </code></pre> <p>I'm not big expert in other languages, but I guess that if these languages do not have such functionality in their menu, there is no way to do that. At least with C++ you can, and I think it is not so much complicated... </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.
    3. VO
      singulars
      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