std::tuple foreach implementation I wrote a "foreach" implementation for std::tuple:

#pragma once

#include &lt;tuple&gt;
/**
 * Callback example:

struct Call{
    float k=0;

    template&lt;typename T, int Index&gt;        // lambda function not efficient than this. Tested -O2 clang, gcc 4.8
    inline void call(T &amp;&amp;t){
        std::cout &lt;&lt; t.h &lt;&lt; " ; " &lt;&lt; "id = " &lt;&lt; Index &lt;&lt; std::endl;
    }
};
*/


namespace TUPLE_ITERATOR{
    template&lt;typename Tuple, int index, int size&gt;
    struct LOOP{
        template &lt;typename Callback&gt;
        static inline void wind(Tuple&amp;&amp; tuple, Callback&amp;&amp; callback){
            callback.template call&lt;decltype(std::get&lt;index&gt;(tuple)), index&gt; (std::get&lt;index&gt;(tuple));
            LOOP&lt;Tuple, index+1, size&gt;::wind( std::forward&lt;Tuple&gt;(tuple), std::forward&lt;Callback&gt;(callback) );
        }
    };

    template&lt;typename Tuple, int size&gt;
    struct LOOP_BACK{
        template &lt;typename Callback&gt;
        static inline void wind_reverse(Tuple&amp;&amp; tuple, Callback&amp;&amp; callback){
            callback.template call&lt;decltype(std::get&lt;size&gt;(tuple)), size&gt;( std::get&lt;size&gt;(tuple) );
            LOOP_BACK&lt;Tuple, size-1&gt;::wind_reverse( std::forward&lt;Tuple&gt;(tuple), std::forward&lt;Callback&gt;(callback) );
        }
    };

    // stop specialization
    template&lt;typename Tuple, int size&gt;
    struct LOOP&lt;Tuple, size, size&gt; {
        template &lt;typename Callback&gt;
        static inline void wind(Tuple&amp;&amp; , Callback&amp;&amp; ){
            // end
        }
    };
    template&lt;typename Tuple&gt;
    struct LOOP_BACK&lt;Tuple, -1&gt;{
        template &lt;typename Callback&gt;
        static inline void wind_reverse(Tuple&amp;&amp; , Callback&amp;&amp; ){
            // end
        }
    };
}

template&lt;typename Tuple, typename Callback&gt;
static void inline iterate_tuple(Tuple&amp;&amp; tuple, Callback&amp;&amp; callback){
    TUPLE_ITERATOR::LOOP&lt; Tuple, 0, std::tuple_size&lt; typename std::decay&lt;Tuple&gt;::type &gt;::value &gt;
            ::template wind&lt;Callback&gt;( std::forward&lt;Tuple&gt;(tuple), std::forward&lt;Callback&gt;(callback) );
}

template&lt;typename Tuple, typename Callback&gt;
static void inline iterate_tuple_back(Tuple&amp;&amp; tuple, Callback&amp;&amp; callback){
    TUPLE_ITERATOR::LOOP_BACK&lt; Tuple, std::tuple_size&lt; typename std::decay&lt;Tuple&gt;::type &gt;::value-1 &gt;
            ::template wind_reverse&lt;Callback&gt;( std::forward&lt;Tuple&gt;(tuple), std::forward&lt;Callback&gt;(callback) );
}

// Call:
// iterate_tuple(Callback(), std::make_tuple(1,2,3,"asdaa"));


But I look at how other folks do that, and I see that they do this in another way. They get an array of indices, and then recursively call the callback function. Is my implementation worse than that? I ask this because if I call tuple_iterator twice, with the same parameters, the compiler starts to use asm "calls". Look HERE, on the red.

void TUPLE_ITERATOR::LOOP&lt;std::tuple&lt;std::pair&lt;int, Data&gt;,
std::pair&lt;int, Data&gt; &gt;, 0, 2&gt;:
Loki's solution does not enforce the order in which the function calls are performed, because the order in which function arguments are evaluated is unspecified. Here's a C++14 solution that ensures the function is called from left to right:

#include &lt;cstddef&gt;
#include &lt;tuple&gt;
#include &lt;utility&gt;

template &lt;typename Tuple, typename F, std::size_t ...Indices&gt;
void for_each_impl(Tuple&amp;&amp; tuple, F&amp;&amp; f, std::index_sequence&lt;Indices...&gt;) {
    using swallow = int[];
    (void)swallow{1,
        (f(std::get&lt;Indices&gt;(std::forward&lt;Tuple&gt;(tuple))), void(), int{})...
    };
}

template &lt;typename Tuple, typename F&gt;
void for_each(Tuple&amp;&amp; tuple, F&amp;&amp; f) {
    constexpr std::size_t N = std::tuple_size&lt;std::remove_reference_t&lt;Tuple&gt;&gt;::value;
    for_each_impl(std::forward&lt;Tuple&gt;(tuple), std::forward&lt;F&gt;(f),
                  std::make_index_sequence&lt;N&gt;{});
}


I use swallow{f(x)...} to force the evaluation order. It works because the order in which the arguments to a brace initializer are evaluated is the order in which they appear. You can then use it like:

#include &lt;iostream&gt;

int main() {
    for_each(std::make_tuple(1, '2', 3.3), [](auto x) {
        std::cout &lt;&lt; x &lt;&lt; std::endl;
    });
}


EDIT

I modified the code so it works on both GCC and Clang. Here's a more in-depth explanation of for_each_impl.

First, we make sure that we call f inside a braced initializer, so the evaluation order is from left to right:

using swallow = int[];
swallow{f(std::get&lt;Indices&gt;(tuple))...};


But then, what if f does not return an integer value? What if it returns void for example? So we use the comma operator to make sure the expression is an integer which can be used inside the braced initializer:

swallow{(f(std::get&lt;Indices&gt;(tuple)), int{})...};


The expression (f(stuff), int{})... is a parameter pack expansion. It expands to (f(stuff_1), int{}), (f(stuff_2), int{}), ..., (f(stuff_n), int{}), so each expression is really an int, except that some side effect has been performed before. Then, to avoid nasty overloads of the comma operator by whatever is returned by f, we insert a void between f(...) and int{}. Since operator,(SomeType, void) can't be overloaded, this ensures that the builtin operator, is used, which is what we want. This might seem overkill, but we do this in highly generic code where we must assume that f(...) could overload operator,:

swallow{(f(std::get&lt;Indices&gt;(tuple)), void(), int{})...};
                                      ^~~~ Make sure the builtin operator, is used


Then, what happens if for_each_impl is sent 0 arguments? We're gonna try to create a 0-sized array, so we must make sure the array always has at least one element in it. We use a dummy int for this:

swallow{1, (f(std::get&lt;Indices&gt;(tuple)), void(), int{})...};
        ^~~~ Now the array always has at least one element in it


We're almost done, but now there's an anoying compiler warning saying "You're creating a temporary array 'swallow' which is never used". To silence it, I cast the swallow{...} to void. Finally, just add perfect forwarding of the Tuple and you're done:

(void)swallow{1, (f(std::get&lt;Indices&gt;(std::forward&lt;Tuple&gt;(tuple))), void(), int{})...};
^^^^^^ Silence warning                ^^^^^^^^^^^^^^^^^^^ Perfect forwarding     


Note that the way I use std::forward here could be unsafe in other circumstances. This is because tuple could be double-moved-from if the function I forwarded it to had different characteristics. Consider:

swallow{f(function_that_moves_from_its_arg&lt;Indices&gt;(std::forward&lt;Tuple&gt;(tuple)))...};


Now, tuple might be moved-from several times:

swallow{
    f(function_that_moves_from_its_arg&lt;Index1&gt;(std::forward&lt;Tuple&gt;(tuple))), // move here
    f(function_that_moves_from_its_arg&lt;Index2&gt;(std::forward&lt;Tuple&gt;(tuple))), // move here
    f(function_that_moves_from_its_arg&lt;Index3&gt;(std::forward&lt;Tuple&gt;(tuple))), // move here
    ...
}


However, I know std::get is a friendly function and so there's should be no problem in doing this. There's an alternative way to do it "safely", but it involves using std::tuple_element and it's more complicated.