fork(2) download
  1. #include <iostream>
  2.  
  3. #if 1 // Not in C++11 // make_index_sequence
  4. #include <cstdint>
  5.  
  6. template <std::size_t...> struct index_sequence {};
  7.  
  8. template <std::size_t N, std::size_t... Is>
  9. struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};
  10.  
  11. template <std::size_t... Is>
  12. struct make_index_sequence<0u, Is...> : index_sequence<Is...> {};
  13.  
  14. #endif // make_index_sequence
  15.  
  16. namespace detail
  17. {
  18. template<std::size_t...Is, typename... Ts>
  19. void helper(index_sequence<Is...>, Ts&&...args)
  20. {
  21. int dummy[] = {0, ((std::cout << "arg " << Is << " = " << args << std::endl), 0)...};
  22. static_cast<void>(dummy);
  23. }
  24.  
  25. }
  26.  
  27. void helper()
  28. {
  29. std::cout << "No args" << std::endl;
  30. }
  31.  
  32. template<typename ... Ts>
  33. void helper(Ts&&... args)
  34. {
  35. detail::helper(make_index_sequence<sizeof...(Ts)>(), std::forward<Ts>(args)...);
  36. }
  37.  
  38. int main()
  39. {
  40. helper();
  41. helper(42, 5, "toto");
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
No args
arg 0 = 42
arg 1 = 5
arg 2 = toto