#include <string>
#include <iostream>
#include <tuple>
#include <vector>
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>(std::forward<T>(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)>());
}
template<typename... Ts>
struct TupleOfVectors
{
std::tuple<std::vector<Ts>...> t;
void do_something_to_each_vec()
{
for_each_in_tuple(t, tuple_vector_functor());
}
struct tuple_vector_functor
{
template<typename T>
void operator () (T const &v)
{
std::cout << "{ ";
bool first = true;
for (auto const& x : v)
{
if (!first) { std::cout << ", "; };
std::cout << x;
first = false;
}
std::cout << " }" << std::endl;
}
};
};
int main()
{
TupleOfVectors<int, double, std::string> tv;
std::get<0>(tv.t).push_back(42);
std::get<0>(tv.t).push_back(1729);
std::get<1>(tv.t).push_back(3.14);
std::get<2>(tv.t).push_back("Hello");
std::get<2>(tv.t).push_back("World");
std::get<2>(tv.t).push_back("of");
std::get<2>(tv.t).push_back("Tuples!");
tv.do_something_to_each_vec();
}
ICAgICNpbmNsdWRlIDxzdHJpbmc+CiAgICAjaW5jbHVkZSA8aW9zdHJlYW0+CiAgICAjaW5jbHVkZSA8dHVwbGU+CiAgICAjaW5jbHVkZSA8dmVjdG9yPgoKICAgIG5hbWVzcGFjZSBkZXRhaWwKICAgIHsKICAgICAgICB0ZW1wbGF0ZTxpbnQuLi4gSXM+CiAgICAgICAgc3RydWN0IHNlcSB7IH07CgogICAgICAgIHRlbXBsYXRlPGludCBOLCBpbnQuLi4gSXM+CiAgICAgICAgc3RydWN0IGdlbl9zZXEgOiBnZW5fc2VxPE4gLSAxLCBOIC0gMSwgSXMuLi4+IHsgfTsKCiAgICAgICAgdGVtcGxhd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sKICAgICAgICAgICAgICAgIGJvb2wgZmlyc3QgPSB0cnVlOwogICAgICAgICAgICAgICAgZm9yIChhdXRvIGNvbnN0JiB4IDogdikKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZiAoIWZpcnN0KSB7IHN0ZDo6Y291dCA8PCAiLCAiOyB9OwogICAgICAgICAgICAgICAgICAgIHN0ZDo6Y291dCA8PCB4OwogICAgICAgICAgICAgICAgICAgIGZpcnN0ID0gZmFsc2U7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGQ6OmNvdXQgPDwgIiB9IiA8PCBzdGQ6OmVuZGw7CiAgICAgICAgICAgIH0KICAgICAgICB9OwogICAgfTsKCiAgICBpbnQgbWFpbigpCiAgICB7CiAgICAgICAgVHVwbGVPZlZlY3RvcnM8aW50LCBkb3VibGUsIHN0ZDo6c3RyaW5nPiB0djsKICAgICAgICBzdGQ6OmdldDwwPih0di50KS5wdXNoX2JhY2soNDIpOwogICAgICAgIHN0ZDo6Z2V0PDA+KHR2LnQpLnB1c2hfYmFjaygxNzI5KTsKCiAgICAgICAgc3RkOjpnZXQ8MT4odHYudCkucHVzaF9iYWNrKDMuMTQpOwoKICAgICAgICBzdGQ6OmdldDwyPih0di50KS5wdXNoX2JhY2soIkhlbGxvIik7CiAgICAgICAgc3RkOjpnZXQ8Mj4odHYudCkucHVzaF9iYWNrKCJXb3JsZCIpOwogICAgICAgIHN0ZDo6Z2V0PDI+KHR2LnQpLnB1c2hfYmFjaygib2YiKTsKICAgICAgICBzdGQ6OmdldDwyPih0di50KS5wdXNoX2JhY2soIlR1cGxlcyEiKTsKCiAgICAgICAgdHYuZG9fc29tZXRoaW5nX3RvX2VhY2hfdmVjKCk7CiAgICB9Cg==