Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to use std::tuple and you have C++ compiler which supports variadic templates, try code bellow (tested with g++4.5). This should be the answer to your question.</p> <pre><code>#include &lt;tuple&gt; // ------------- 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; {}; // ----------- FOR EACH ----------------- template&lt;typename Func, typename Last&gt; void for_each_impl(Func&amp;&amp; f, Last&amp;&amp; last) { f(last); } template&lt;typename Func, typename First, typename ... Rest&gt; void for_each_impl(Func&amp;&amp; f, First&amp;&amp; first, Rest&amp;&amp;...rest) { f(first); for_each_impl( std::forward&lt;Func&gt;(f), rest...); } template&lt;typename Func, int ... Indexes, typename ... Args&gt; void for_each_helper( Func&amp;&amp; f, index_tuple&lt;Indexes...&gt;, std::tuple&lt;Args...&gt;&amp;&amp; tup) { for_each_impl( std::forward&lt;Func&gt;(f), std::forward&lt;Args&gt;(std::get&lt;Indexes&gt;(tup))...); } template&lt;typename Func, typename ... Args&gt; void for_each( std::tuple&lt;Args...&gt;&amp; tup, Func&amp;&amp; f) { for_each_helper(std::forward&lt;Func&gt;(f), typename make_indexes&lt;Args...&gt;::type(), std::forward&lt;std::tuple&lt;Args...&gt;&gt;(tup) ); } template&lt;typename Func, typename ... Args&gt; void for_each( std::tuple&lt;Args...&gt;&amp;&amp; tup, Func&amp;&amp; f) { for_each_helper(std::forward&lt;Func&gt;(f), typename make_indexes&lt;Args...&gt;::type(), std::forward&lt;std::tuple&lt;Args...&gt;&gt;(tup) ); } </code></pre> <p>boost::fusion is another option, but it requires its own tuple type: boost::fusion::tuple. Lets better stick to the standard! Here is a test:</p> <pre><code>#include &lt;iostream&gt; // ---------- FUNCTOR ---------- struct Functor { template&lt;typename T&gt; void operator()(T&amp; t) const { std::cout &lt;&lt; t &lt;&lt; std::endl; } }; int main() { for_each( std::make_tuple(2, 0.6, 'c'), Functor() ); return 0; } </code></pre> <p>the power of variadic templates!</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