fork download
  1. #include <cstdio>
  2. #include <utility>
  3.  
  4. using namespace std;
  5.  
  6. struct Heavy
  7. {
  8. Heavy() { printf("h "); }
  9. Heavy(const Heavy&) { printf("H "); }
  10. };
  11.  
  12. template<int N, class... Args> struct getter;
  13. template<class A, class... Args> struct getter<0,A,Args...>
  14. {
  15. typedef A type;
  16. static type get(A a, Args&&...) { printf("0 "); return a; }
  17. };
  18. template<int N, class A, class... Args> struct getter<N,A,Args...>
  19. {
  20. typedef getter<N-1,Args...> base;
  21. typedef typename base::type type;
  22. static type get(A a, Args&&... args)
  23. {
  24. printf("%d ", N);
  25. return base::get(forward<Args>(args)...);
  26. }
  27. };
  28.  
  29. template<int N, class... Args>
  30. typename getter<N,Args...>::type
  31. get(Args&&... args)
  32. {
  33. printf("go ");
  34. return getter<N,Args...>::get(forward<Args>(args)...);
  35. }
  36.  
  37. int main()
  38. {
  39. get<4>(0,1,2,3,4, Heavy(), 6,7,8,9);
  40. printf("\n");
  41. get<5>(0,1,2,3,4, Heavy(), 6,7,8,9);
  42. printf("\n");
  43. get<9>(0,1,2,3,4, Heavy(), 6,7,8,9);
  44. printf("\n");
  45. }
  46.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
h go 4 3 2 1 0 
h go 5 4 3 2 1 H 0 H 
h go 9 8 7 6 5 H 4 3 2 1 0