Long time back I had seen a non-recursive implementation to get the last value/type from a type sequence/value sequence. It has a nice property, that the number of template instantiated is independent (and constant) of the number of elements the sequence contains.
The implementation is simple, as follows
// a struct that eats anything and everything
struct eat { template<class T> eat(T&&) {} };
// generates V matching with U
template<class U, class V> struct match { using type = V; };
template<class... X> struct back_
{
template<class U>
static U&& get(typename match<X, eat>::type..., U&& u)
{
return static_cast<U&&>(u); // forward
}
};
// simple macro to avoid repetition for trailing return type.
#define RETURNS(exp) -> decltype(exp) { return exp; }
// get the last value in meta O(1)
template<class T, class... Ts>
auto back(T&& t, Ts&&... ts) RETURNS( back_<Ts...>::get(static_cast<T&&>(t), static_cast<Ts&&>(ts)...))
It uses a simple fact that given a variadic type X... the compiler can non-recursively generate another type T as many as Xs are there.
So, I want to know is there a way to extend it to implement at_c or nth function with constant number of instantiated templates (independent of number of elements).
It also may be phrased as, give a variadic type X... and some integer N, is it possible to non-recursively generate a sub-sequence of X... consisting of N elements?