Error spliting an std::index_sequence

Viewed 2176

I'm trying to split an index_sequence into two halves. For that, I generate an index_sequence with the lower half and use it to skip the leading elements on the full index_sequence. The following is a minimal test case that represents what I'm trying to achieve:

template <int ...I>
struct index_sequence {};

template <int ...I, int ...J>
void foo(index_sequence<I...>, index_sequence<I..., J...>)
{}

int main()
{
    foo(index_sequence<0>{}, index_sequence<0, 1>{});
}

I've tried this with the latest versions of Clang, GCC and MSVC, and they all fail to deduce J.... Is this allowed by the standard? If not, why and what would be a good way to achieve my intent?

5 Answers

I needed to split an index_sequence into a head and tail at a particular point and this was the implementation that I came up with:

template<size_t N, typename Lseq, typename Rseq>
struct split_sequence_impl;

template<size_t N, size_t L1, size_t...Ls, size_t...Rs>
struct split_sequence_impl<N,index_sequence<L1,Ls...>,index_sequence<Rs...>>  { 
  using next = split_sequence_impl<N-1,index_sequence<Ls...>,index_sequence<Rs...,L1>>;
  using head = typename next::head;
  using tail = typename next::tail;
};

template<size_t L1, size_t...Ls, size_t...Rs>
struct split_sequence_impl<0,index_sequence<L1,Ls...>,index_sequence<Rs...>> {
  using tail = index_sequence<Ls...>;
  using head = index_sequence<Rs...,L1>;
};

template<typename seq, size_t N> 
using split_sequence = split_sequence_impl<N-1,seq,empty_sequence>;

template<typename seq, size_t N>
using sequence_head_t = typename split_sequence<seq,N>::head;

template<typename seq, size_t N>
using sequence_tail_t = typename split_sequence<seq,N>::tail;
Related