#include <string>
#include <iostream>
#include <tuple>
namespace detail
{
template<int... Is>
struct seq { };
template<int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template<int... Is>
struct gen_seq<0, Is...> : seq<Is...> { };
template<typename T, typename F, int... Is>
void for_each(T&& t, F f, seq<Is...>)
{
auto l = { (f(std::get<Is>(t)), 0)... };
}
}
template<typename... Ts, typename F>
void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
{
detail::for_each(t, f, detail::gen_seq<sizeof...(Ts)>());
}
struct my_functor
{
template<typename T>
void operator () (T&& t)
{
std::cout << t << std::endl;
}
};
int main()
{
std::tuple<int, double, std::string> t(42, 3.14, "Hello World!");
for_each_in_tuple(t, my_functor());
}
ICAgICNpbmNsdWRlIDxzdHJpbmc+CiAgICAjaW5jbHVkZSA8aW9zdHJlYW0+CiAgICAjaW5jbHVkZSA8dHVwbGU+CgogICAgbmFtZXNwYWNlIGRldGFpbAogICAgewogICAgICAgIHRlbXBsYXRlPGludC4uLiBJcz4KICAgICAgICBzdHJ1Y3Qgc2VxIHsgfTsKCiAgICAgICAgdGVtcGxhdGU8aW50IE4sIGludC4uLiBJcz4KICAgICAgICBzdHJ1Y3QgZ2VuX3NlcSA6IGdlbl9zZXE8TiAtIDEsIE4gLSAxLCBJcy4uLj4geyB9OwoKICAgICAgICB0ZW1wbGF0ZTxpbnQuLi4gSXM+CiAgICAgICAgc3RydWN0IGdlbl9zZXE8MCwgSXMuLi4+IDogc2VxPElzLi4uPiB7IH07CgogICAgICAgIHRlbXBsYXRlPHR5cGVuYW1lIFQsIHR5cGVuYW1lIEYsIGludC4uLiBJcz4KICAgICAgICB2b2lkIGZvcl9lYWNoKFQmJiB0LCBGIGYsIHNlcTxJcy4uLj4pCiAgICAgICAgewogICAgICAgICAgICBhdXRvIGwgPSB7IChmKHN0ZDo6Z2V0PElzPih0KSksIDApLi4uIH07CiAgICAgICAgfQogICAgfQoKICAgIHRlbXBsYXRlPHR5cGVuYW1lLi4uIFR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