fork download
  1. #include <vector>
  2. #include <array>
  3. #include <numeric>
  4. #include <iostream>
  5.  
  6. template<int ... I>
  7. struct IndexSeq
  8. {
  9. };
  10.  
  11. template<int T, typename T> struct MakeIndexSeqImpl;
  12. template<int T, int ... H>
  13. struct MakeIndexSeqImpl<T, IndexSeq<H ...>>
  14. {
  15. typedef IndexSeq<H..., T> type;
  16. };
  17.  
  18. template<int N, int Init, int Step>
  19. struct MakeIndexSeq
  20. {
  21. typedef typename MakeIndexSeqImpl<(N - 1) * Step + Init, typename MakeIndexSeq<N - 1, Init, Step>::type>::type type;
  22. };
  23.  
  24. template<int Init, int Step>
  25. struct MakeIndexSeq<1, Init, Step>
  26. {
  27. typedef IndexSeq<Init> type;
  28. };
  29.  
  30. template<int ... I>
  31. constexpr std::array<int, sizeof ... (I)> MakeSeqImpl(IndexSeq<I ...> seq)
  32. {
  33. return std::array<int, sizeof ... (I)>{I ...};
  34. }
  35.  
  36. template<int I, int Step = 1, int Init = 0>
  37. constexpr std::array<int, I> MakeSeq()
  38. {
  39. return MakeSeqImpl(typename MakeIndexSeq<I, Init, Step>::type());
  40. }
  41.  
  42. int main()
  43. {
  44. for (auto i : MakeSeq<20, 10, 10>())
  45. {
  46. std::cout << i << ' ';
  47. }
  48. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200