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