I would like to know what is the correct way to retrieve the value of a variadic template constant argument at position N (N is known at compile time). For instance, let's say you have a template that receives a variadic number of function pointers as arguments, and you need to retrieve the second function pointer. For now, all I've been able to come up with is this...
typedef int (*func)(int);
template< func... F >
struct testme
{
inline int getme(int p) const
{
return std::array< func , sizeof... (F) >{F...}[1](p);
}
};
... which, needless to say, is very hackish. Is there a better way to do this? Thanks.
EDIT:
Based on typedeftemplate's code, I made a version that can accept any type as the variadic template argument. It has been tested to work on an experimental build of GCC 4.6. I figured out it could be useful to somebody else so there it is...
template< std::size_t I, typename T, T... Args >
struct va_lookup;
template< std::size_t I, typename T, T Arg, T... Args >
struct va_lookup< I, T, Arg, Args... >
{
static_assert(I <= sizeof... (Args), "index is out of bound");
static constexpr T value = va_lookup< I - 1, T, Args... >::value;
};
template< typename T, T Arg, T... Args >
struct va_lookup< 0, T, Arg, Args... >
{
static constexpr T value = Arg;
};