我一直在尝试实现某种线程池。我需要在Queue中获取一些任务,这些任务可能会有一些参数。
template <typename FUNCTION, typename... ARGS>
void push_function(const FUNCTION &function, const ARGS &...args){
push_function([function, args...]{ function(args...); });
}
template <typename FUNCTION>
void push_function(FUNCTION &function){
{
std::unique_lock<std::mutex> lock(queue_mutex);
queue.push(std::function<void()>(function));
}
cv.notify_one();
}由于这一行,我得到了错误:must use '.*' or '->' to call pointer-to-member function in 'function(...)'...
push_function([function, args...]{ function(args...); });发布于 2021-10-02 23:57:30
您正在推送一个非静态成员函数。
std::invoke来统一对待它们。尝试:
template <typename FUNCTION, typename... ARGS>
void push_function(const FUNCTION &function, const ARGS &...args){
push_function([function, args...]{ std::invoke(function,args...); });
}并将对象的指针作为第二个参数传递给push函数。
如果您缺少std invoke,请使用:
template <typename FUNCTION, typename... ARGS>
void push_function(const FUNCTION &function, const ARGS &...args){
push_function([function, args...]{ std::cref(function)(args...); });
}作为获得相同行为的一种方式。
https://stackoverflow.com/questions/69421068
复制相似问题