I want to compute the exclusive prefix sum (scan) of the indices of a std::index_sequence
, but I'm unsure where to start. I've investigated the implementation of std::make_index_sequence
looking for a generalization, but it is mysterious to me.
How can I implement exclusive_scan_index_sequence
below to make the program succeed?
#include <type_traits>
#include <cassert>
#include <cstddef>
// define something like std::index_sequence
template<size_t... Indices>
struct index_sequence {};
// compute the exclusive scan of IndexSequence
// initializing the first value in the result sequence to Init
template<size_t Init, class IndexSequence>
struct exclusive_scan_index_sequence;
template<size_t Init, size_t... Indices>
struct exclusive_scan_index_sequence<Init,index_sequence<Indices...>>
{
// what goes here?
};
int main()
{
using ones = index_sequence<1,1,1,1,1>;
using scanned = exclusive_scan_index_sequence<0,ones>;
using expected = index_sequence<0,1,2,3,4>;
assert((std::is_same<expected,scanned>::value));
return 0;
}