fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. void helper()
  5. {
  6. std::cout << "no args" << std::endl;
  7. }
  8.  
  9. template<size_t N, typename T>
  10. void helper(T&& arg)
  11. {
  12. std::cout << "arg " << N << " = " << arg << std::endl;
  13. }
  14.  
  15. template<size_t N, typename T, typename... Args>
  16. void helper(T&& arg, Args&&... args)
  17. {
  18. helper<N>(std::forward<T>(arg));
  19. helper<N+1, Args...>(std::forward<Args>(args)...);
  20. }
  21.  
  22. template<typename T, typename... Args>
  23. void helper(T&& arg, Args&& ... args)
  24. {
  25. helper<0>(std::forward<T>(arg), std::forward<Args>(args)... );
  26. }
  27.  
  28. int main()
  29. {
  30. helper();
  31. std::cout << '\n';
  32.  
  33. helper("single");
  34. std::cout << '\n';
  35.  
  36. helper("one", 2U);
  37. std::cout << '\n';
  38.  
  39. helper(1,"two", 3.0, 4L);
  40. std::cout << '\n';
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
no args

arg 0 = single

arg 0 = one
arg 1 = 2

arg 0 = 1
arg 1 = two
arg 2 = 3
arg 3 = 4