fork download
  1. using size_type = unsigned;
  2.  
  3. using index_type = size_type;
  4.  
  5. template<typename T>
  6. using eval = typename T::type;
  7.  
  8. template<index_type ... args>
  9. struct index_list
  10. {
  11. using type = index_list;
  12. };
  13.  
  14. template <typename, typename> struct multiply;
  15. template <index_type... indices, index_type... tail>
  16. struct multiply<index_list<indices...>,
  17. index_list<tail... >> : index_list<indices..., (sizeof...(indices)+indices)...,
  18. (2*sizeof...(indices)+indices)...,
  19. (3*sizeof...(indices)+indices)...,
  20. (4*sizeof...(indices)+tail)...> {};
  21. template <index_type N>
  22. struct make_index_list :
  23. multiply< eval<make_index_list<N/4>>,
  24. eval<make_index_list<N%4>> > {};
  25.  
  26. template <> struct make_index_list<3> : index_list<0, 1, 2> {};
  27. template <> struct make_index_list<2> : index_list<0, 1> {};
  28. template <> struct make_index_list<1> : index_list<0> {};
  29. template <> struct make_index_list<0> : index_list<> {};
  30.  
  31. template< void(*)(index_type),
  32. size_type len,
  33. typename = eval<make_index_list<len>>> struct repeat;
  34.  
  35. template< void(*ptr)(index_type), size_type len,
  36. index_type... indices >
  37. struct repeat<ptr, len, index_list<indices...>>
  38. {
  39. static void run()
  40. {
  41. bool _[]{ (ptr(indices), false)... };
  42. }
  43. };
  44.  
  45. #include <iostream>
  46.  
  47. void foo(index_type i)
  48. {
  49. std::cout << i << '\n';
  50. }
  51.  
  52. int main()
  53. {
  54. repeat<foo, 7>::run();
  55. }
  56.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6