【发布时间】:2019-08-04 21:18:48
【问题描述】:
我尝试使用this 代码,但它不适用于类成员函数。有没有办法为类成员函数生成一个包装器并将其称为非类成员的包装器?
#include <functional>
template <typename F>
class wrapper;
template <typename F, typename... Args>
typename std::result_of<F(Args...)>::type execute_helper (F&& f, Args&&... args) {
return std::forward<F>(f)( std::forward<Args>(args)...);
}
template<typename Ret, typename... Args>
class wrapper <std::function<Ret(Args...)>>
{
std::function<Ret(Args...)> m_f;
public:
wrapper( std::function<Ret(Args...)> f ): m_f(f) {}
Ret operator()(Args... args) const {
return execute_helper (m_f, args...);
}
};
【问题讨论】:
-
为什么还需要
execute_helper?只需写出m_f(args...)。另外,为什么这不适用于成员函数(您得到的确切错误是什么,以及产生该错误的代码段)?这个包装器做了哪些std::function没有做的事情?
标签: c++ templates variadic-templates variadic-functions