I have come across this code in this answer and I can not explain what it does exactly and how I might use it as a compile time sequence of integers.
template <size_t ...I>
struct index_sequence {};
template <size_t N, size_t ...I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};
template <size_t ...I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};
I have found similar implementations before, namely here and here, but never knew how to understand the syntax.
I don´t understand the two template struct definitions for make_index_sequence
, in particular the parts with public
(e.g. : public make_index_sequence<N - 1, N - 1, I...>
). May someone explain this to me?
Is there any inheritance happening here? How does the recursion work? Where is the sequence stored?
EDIT: An example usage for an integer sequence generated by these templates is to generate indices for accessing a std::tuple<>
. The generator will generate an index for each element in the tuple at compile time.