fork(34) download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. struct Functor
  6. {
  7. template<typename T>
  8. void operator()(T& t) const { std::cout << t << std::endl; }
  9. };
  10.  
  11. template<std::size_t I = 0, typename FuncT, typename... Tp>
  12. inline typename std::enable_if<I == sizeof...(Tp), void>::type
  13. for_index(int, std::tuple<Tp...> &, FuncT)
  14. { }
  15.  
  16. template<std::size_t I = 0, typename FuncT, typename... Tp>
  17. inline typename std::enable_if<I < sizeof...(Tp), void>::type
  18. for_index(int index, std::tuple<Tp...>& t, FuncT f)
  19. {
  20. if (index == 0) f(std::get<I>(t));
  21. for_index<I + 1, FuncT, Tp...>(index-1, t, f);
  22. }
  23.  
  24. int main() {
  25. auto t = make_tuple(1, 2, "abc", "def", 4.0f);
  26. int i = 2; // for example
  27. for_index(i, t, Functor());
  28. return 0;
  29. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
abc