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!
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, "!" );
}
Here's a way to do it with inheritance:
template <std::size_t, typename>
struct make_reverse_index_sequence_helper;
template <std::size_t N, std::size_t...NN>
struct make_reverse_index_sequence_helper<N, std::index_sequence<NN...>>
: std::index_sequence<(N - NN)...> {};
template <size_t N>
struct make_reverse_index_sequence
: make_reverse_index_sequence_helper<N - 1,
decltype(std::make_index_sequence<N>{})> {};
The helper struct is used to deduce the parameters and apply the subtraction. It can be used just like std::make_index_sequence because it derives from std::index_sequence, as you can see here:
std::index_sequence<4, 3, 2, 1, 0> x = make_reverse_index_sequence<5>{};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With