fork download
  1. #include <cstddef>
  2. namespace std {
  3.  
  4. template <typename T_, T_...>
  5. struct value_sequence {
  6. };
  7.  
  8. template <size_t... Is_>
  9. using index_sequence = value_sequence<size_t, Is_...>;
  10.  
  11. namespace detail {
  12.  
  13. template <typename Seq_, size_t N_>
  14. struct make_index_seq_impl_even;
  15.  
  16. template <size_t... Is_, size_t N_>
  17. struct make_index_seq_impl_even<value_sequence<size_t, Is_...>, N_> {
  18. using type = value_sequence<size_t, Is_..., (Is_ + N_)...>;
  19. };
  20.  
  21. template <typename Seq_, size_t N_>
  22. struct make_index_seq_impl_odd;
  23.  
  24. template <size_t... Is_, size_t N_>
  25. struct make_index_seq_impl_odd<value_sequence<size_t, Is_...>, N_> {
  26. using type = value_sequence<size_t, Is_..., (Is_ + N_)..., (2 * N_)>;
  27. };
  28.  
  29. template <size_t N_, bool IsOdd_ = (N_ % 2)>
  30. struct make_index_seq_impl {
  31. using type = typename make_index_seq_impl_even<
  32. typename make_index_seq_impl<N_ / 2>::type, N_ / 2>::type;
  33. };
  34.  
  35. template <size_t N_>
  36. struct make_index_seq_impl<N_, true> {
  37. using type = typename make_index_seq_impl_odd<
  38. typename make_index_seq_impl<N_ / 2>::type, N_ / 2>::type;
  39. };
  40.  
  41. template <>
  42. struct make_index_seq_impl<0, false> {
  43. using type = value_sequence<size_t>;
  44. };
  45.  
  46. namespace {
  47. template <typename T>
  48. constexpr inline
  49. T
  50. identity_transform(size_t i) {
  51. return i;
  52. }
  53.  
  54. }
  55.  
  56. template <typename IndexSeq_, typename T_, T_ (*F_)(size_t) = identity_transform<T_>>
  57. struct transform_seq;
  58.  
  59. template <size_t... Is_, typename T_, T_(*F_)(size_t)>
  60. struct transform_seq<index_sequence<Is_...>, T_, F_> {
  61. using type = value_sequence<T_, F_(Is_)...>;
  62. };
  63.  
  64. } // namespace detail
  65.  
  66.  
  67. template <size_t N_>
  68. using make_index_sequence = typename detail::make_index_seq_impl<N_>::type;
  69.  
  70. template <typename... Ts_>
  71. using index_sequence_for = make_index_sequence<sizeof...(Ts_)>;
  72.  
  73. template <typename IndexSeq_, typename T_, T_ (*F_)(size_t) = detail::identity_transform<T_>>
  74. using transform_sequence = typename detail::transform_seq<IndexSeq_, T_, F_>::type;
  75.  
  76. template <typename T_, T_ N_>
  77. using make_integer_sequence = typename detail::transform_seq<make_index_sequence<N_>, T_>::type;
  78. //using make_integer_sequence = transform_sequence<make_index_sequence<N_>, T_>>;
  79.  
  80. // ------------------------------------------------------------------------- //
  81.  
  82. }
  83.  
  84. #include <iostream>
  85.  
  86. template <size_t... Is>
  87. size_t
  88. size(std::index_sequence<Is...>) {
  89. return sizeof...(Is);
  90. }
  91.  
  92. template <typename... Ts>
  93. void
  94. swallow(Ts &&...) {
  95. }
  96.  
  97. void
  98. print(size_t n) {
  99. std::cout << n << " ";
  100. }
  101.  
  102. template <size_t... Is>
  103. void
  104. print(std::index_sequence<Is...>) {
  105. swallow((print(Is), 0)...);
  106. std::cout << "\n";
  107. }
  108.  
  109. constexpr
  110. size_t
  111. mul2(size_t i) {
  112. return 2 * i;
  113. }
  114.  
  115. int
  116. main() {
  117. using Seq = std::make_index_sequence<2345>;
  118. std::cout << "Seq size: " << size(Seq()) << "\n";
  119. using Seq11 = std::make_index_sequence<11>;
  120. print(Seq11());
  121. using Seq11Even = std::transform_sequence<Seq11, size_t, mul2>;
  122. // using Seq11Even = std::transform_sequence<Seq11, size_t, [](const size_t i) { return 2 * i; }>;
  123. print(Seq11Even());
  124. return 0;
  125. }
  126.  
  127.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Seq size: 2345
10 9 8 7 6 5 4 3 2 1 0 
20 18 16 14 12 10 8 6 4 2 0