使用非常量表达式作为模板参数
我有这个结构来访问可变参数模板的参数:
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;
}
This is a follow up on How do I get the argument types of a function pointer in a variadic template class?
I have this struct to access the arguments of a variadic template:
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 access the type of an argument of Args 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 through the arguments to be able to handle an arbitrary number of arguments. The following doesn't 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要按照以下方式进行编译时迭代
You'd need to do a compile-time iteration along the lines of