Using a non-const expression as a template parameter

This is a continuation of How to get function pointer argument types in a variational pattern class?

I have this structure for accessing variational pattern arguments:

template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; 

And I refer to the Args argument type with

 typedef function<void(Args...)> fun; std::cout << std::is_same<int, typename function_traits<fun>::template arg<0>::type>::value << std::endl; 

However, I would like to iterate over the arguments in order to be able to handle an arbitrary number of arguments. The following steps do not work, but to illustrate what I want:

 for (int i = 0; i < typename function_traits<fun>::nargs ; i++){ std::cout << std::is_same<int, typename function_traits<fun>::template arg<i>::type>::value << std::endl; } 
+3
c ++ c ++ 11 functor function-pointers variadic-templates
source share
1 answer

You need to iterate over compilation time by line

 template <typename fun, size_t i> struct print_helper { static void print() { print_helper<fun, i-1>::print(); std::cout << std::is_same<int, typename function_traits<fun>::template arg<i-1>::type>::value << std::endl; } }; template <typename fun> struct print_helper<fun,0> { static void print() {} }; template <typename fun> void print() { print_helper<fun, function_traits<fun>::nargs>::print(); } 
+5
source share

All Articles