I would like to know what is the correct way to get the value of the argument argument of the variational pattern at position N (N is known at compile time). For example, suppose you have a template that receives a variable number of function pointers as arguments, and you need to get a second function pointer. So far, all I could come up with is ...
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, of course, is very hacker. Is there a better way to do this? Thanks.
EDIT:
Based on the typedeftemplate code, I made a version that can accept any type as an argument to the variational template. It has been tested to work on the experimental build of GCC 4.6. I realized that this might be useful to someone else, so this ...
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; };
source share