As a baby example, you can imagine something like this - just a tuple of one fixed type for now:
template <int N> struct MyTuple : MyTuple<N - 1>
{
T data;
};
template <> struct MyTuple<0> { };
The real-world solution would of course have variadic template parameters for the data types, and would also provide a variadic constructor, constructing data
with the first element and passing the remaining elements to the base constructor.
Now we can try and access the i
th element:
template <int K> struct get_impl
{
template <int N> static T & get(MyTuple<N> & t)
{
return get_impl<K - 1>::get(static_cast<MyTuple<N - 1>&>(t));
}
};
template <> struct get_impl<0>
{
template <int N> static T & get(MyTuple<N> & t)
{
return t.data;
}
};
The key here is to have a specialization when K = 0
which extracts the actual element, and to cast up the inheritance hierarchy until you're there. Finally, we slingshot the tuple type deduction through a function template:
template <int K, int N> T & get(MyTuple<N> & t)
{
return get_impl<K>::get(t);
}