【发布时间】:2012-02-23 01:23:49
【问题描述】:
这是对How do I get the argument types of a function pointer in a variadic template class?的跟进
我有这个结构来访问可变参数模板的参数:
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;
};
};
并且我使用
访问 Args 的参数类型typedef function<void(Args...)> fun;
std::cout << std::is_same<int, typename function_traits<fun>::template arg<0>::type>::value << std::endl;
但是,我想遍历参数以便能够处理任意数量的参数。以下不起作用,但为了说明我想要的:
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;
}
【问题讨论】:
标签: c++ c++11 function-pointers functor variadic-templates