3

Who knows how to implement C++ std::make_index_sequence reverse version. To get - make_index_sequence_reverse<int, 5> = <4,3,2,1,0>. Thank you!

max66
  • 63,246
  • 10
  • 70
  • 107

1 Answers1

10

IMHO, there is no reason for a index_sequence_reverse: std::index_sequence support sequences of indexes and are order neutral (or even without order).

If you can use std::make_index_sequence, for a makeIndexSequenceReverse you can make something as follows

#include <utility>
#include <type_traits>

template <std::size_t ... Is>
constexpr auto indexSequenceReverse (std::index_sequence<Is...> const &)
   -> decltype( std::index_sequence<sizeof...(Is)-1U-Is...>{} );

template <std::size_t N>
using makeIndexSequenceReverse
   = decltype(indexSequenceReverse(std::make_index_sequence<N>{}));

int main ()
 {
   static_assert( std::is_same<std::index_sequence<4U, 3U, 2U, 1U, 0U>,
      makeIndexSequenceReverse<5U>>::value, "!" );
 }
max66
  • 63,246
  • 10
  • 70
  • 107
  • This was not the OP's question, but I would like to note something here: Inverting a sequence with `sizeof...(Is)-1u-Is` only works if the index sequence covers the full range of the original type sequence (e.g., from a tuple). If the index sequence is only a subset, one must consider the size of the original type sequence and the base index of the subset. – klaus triendl May 31 '22 at 08:15