I found a for_each loop for tuples that just iterates through the elements and passes them to the function.
namespace std { template<int I, class Tuple, typename F> struct for_each_impl { static void for_each(const Tuple& t, F f) { for_each_impl<I - 1, Tuple, F>::for_each(t, f); f(get<I>(t)); } }; template<class Tuple, typename F> struct for_each_impl<0, Tuple, F> { static void for_each(const Tuple& t, F f) { f(get<0>(t)); } }; template<class Tuple, typename F> void for_each(const Tuple& t, F f) { for_each_impl<tuple_size<Tuple>::value - 1, Tuple, F>::for_each(t, f); } }
.
auto t = std::make_tuple(Foo(),Bar(),Baz()); std::for_each(t,[](???){});
Is it possible to have such a common function?
std::for_each(t,[](T &&t){t.foo();});
In the end, I just want to have something that works with every tuple.
std::get<0>(t).foo(); std::get<1>(t).foo(); std::get<2>(t).foo(); ...
Maybe it's easier with macros?
source share