iterator over std::tuple code example
Example 1: iterator on std::tuple
#include <tuple>
#include <iostream>
int main()
{
std::tuple t{42, 'a', 4.2};
std::apply([](auto&&... args) {((std::cout << args << '\n'), ...);}, t);
}
Example 2: iterator on std::tuple
{
auto tup = std::make_tuple(0, 'a', 3.14);
template for (auto elem : tup)
std::cout << elem << std::endl;
}
Example 3: iterator on std::tuple
#include <tuple>
#include <utility>
template<std::size_t N>
struct tuple_functor
{
template<typename T, typename F>
static void run(std::size_t i, T&& t, F&& f)
{
const std::size_t I = (N - 1);
switch(i)
{
case I:
std::forward<F>(f)(std::get<I>(std::forward<T>(t)));
break;
default:
tuple_functor<I>::run(i, std::forward<T>(t), std::forward<F>(f));
}
}
};
template<>
struct tuple_functor<0>
{
template<typename T, typename F>
static void run(std::size_t, T, F){}
};