fork download
  1. #include <array>
  2. #include <iostream>
  3. #include <utility>
  4.  
  5. template<int VRange, int VRepCount, int VValueRIndex = VRepCount> class
  6. t_Looper
  7. {
  8. public: template<typename TAction> static void
  9. process(::std::array<int, VRepCount> & values, TAction && action)
  10. {
  11. for(;;)
  12. {
  13. t_Looper<VRange, VRepCount, VValueRIndex - 1>::process(values, ::std::forward<TAction>(action));
  14. auto & value{values[VRepCount - VValueRIndex]};
  15. if((VRange - 1) != value)
  16. {
  17. ++value;
  18. }
  19. else
  20. {
  21. value = 0;
  22. break;
  23. }
  24. }
  25. }
  26. };
  27.  
  28. template<int VRange, int VRepCount> class
  29. t_Looper<VRange, VRepCount, 0>
  30. {
  31. private: template<int... VIndexes, typename TAction> static void
  32. invoke(::std::integer_sequence<int, VIndexes...>, ::std::array<int, VRepCount> const & values, TAction && action)
  33. {
  34. action(values[VIndexes]...);
  35. }
  36.  
  37. public: template<typename TAction> static void
  38. process(::std::array<int, VRepCount> & values, TAction && action)
  39. {
  40. invoke(::std::make_integer_sequence<int, VRepCount>(), values, ::std::forward<TAction>(action));
  41. }
  42. };
  43.  
  44. template<int VRange, int VRepCount, typename TAction> void
  45. multiloop(TAction && action)
  46. {
  47. ::std::array<int, VRepCount> values{};
  48. t_Looper<VRange, VRepCount>::process(values, ::std::forward<TAction>(action));
  49. }
  50.  
  51. int main()
  52. {
  53. multiloop<3, 2>([](int i, int j){::std::cout << i << " " << j << ::std::endl;});
  54. multiloop<3, 4>([](int i, int j, int k, int l){::std::cout << i << " " << j << " " << k << " " << l << ::std::endl;});
  55. return(0);
  56. }
  57.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
0 0 0 0
0 0 0 1
0 0 0 2
0 0 1 0
0 0 1 1
0 0 1 2
0 0 2 0
0 0 2 1
0 0 2 2
0 1 0 0
0 1 0 1
0 1 0 2
0 1 1 0
0 1 1 1
0 1 1 2
0 1 2 0
0 1 2 1
0 1 2 2
0 2 0 0
0 2 0 1
0 2 0 2
0 2 1 0
0 2 1 1
0 2 1 2
0 2 2 0
0 2 2 1
0 2 2 2
1 0 0 0
1 0 0 1
1 0 0 2
1 0 1 0
1 0 1 1
1 0 1 2
1 0 2 0
1 0 2 1
1 0 2 2
1 1 0 0
1 1 0 1
1 1 0 2
1 1 1 0
1 1 1 1
1 1 1 2
1 1 2 0
1 1 2 1
1 1 2 2
1 2 0 0
1 2 0 1
1 2 0 2
1 2 1 0
1 2 1 1
1 2 1 2
1 2 2 0
1 2 2 1
1 2 2 2
2 0 0 0
2 0 0 1
2 0 0 2
2 0 1 0
2 0 1 1
2 0 1 2
2 0 2 0
2 0 2 1
2 0 2 2
2 1 0 0
2 1 0 1
2 1 0 2
2 1 1 0
2 1 1 1
2 1 1 2
2 1 2 0
2 1 2 1
2 1 2 2
2 2 0 0
2 2 0 1
2 2 0 2
2 2 1 0
2 2 1 1
2 2 1 2
2 2 2 0
2 2 2 1
2 2 2 2