fork(50) download
  1. #include <string>
  2. #include <iostream>
  3. #include <tuple>
  4.  
  5. namespace detail
  6. {
  7. template<int... Is>
  8. struct seq { };
  9.  
  10. template<int N, int... Is>
  11. struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
  12.  
  13. template<int... Is>
  14. struct gen_seq<0, Is...> : seq<Is...> { };
  15.  
  16. template<typename T, typename F, int... Is>
  17. void for_each(T&& t, F f, seq<Is...>)
  18. {
  19. auto l = { (f(std::get<Is>(t)), 0)... };
  20. }
  21. }
  22.  
  23. template<typename... Ts, typename F>
  24. void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
  25. {
  26. detail::for_each(t, f, detail::gen_seq<sizeof...(Ts)>());
  27. }
  28.  
  29. struct my_functor
  30. {
  31. template<typename T>
  32. void operator () (T&& t)
  33. {
  34. std::cout << t << std::endl;
  35. }
  36. };
  37.  
  38. int main()
  39. {
  40. std::tuple<int, double, std::string> t(42, 3.14, "Hello World!");
  41. for_each_in_tuple(t, my_functor());
  42. }
  43.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
42
3.14
Hello World!