这是对此问题的跟进:Generic functor for functions with any argument list
我有这个仿函数类(完整代码见上面的链接):
template<typename... ARGS>
class Foo
{
std::function<void(ARGS...)> m_f;
public:
Foo( std::function<void(ARGS...)> f ) : m_f(f) {}
void operator()(ARGS... args) const { m_f(args...); }
};
在operator()中,我可以使用递归"peeling"函数轻松访问args ...如http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates所述
我的问题是:我想在构造函数中访问f的参数类型,即ARGS ....显然我无法访问值,因为到目前为止还没有,但参数类型列表以某种方式埋没在f中,不是吗?
1 回答
您可以编写
function_traits
类,如下所示,以发现参数类型,返回类型和参数数量:测试代码:
演示:http://ideone.com/YeN29