[英]No matching function to call: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (…)’, e.g. ‘(… ->* f) (…)’
I'm trying to call a non-static member function from a thread pool.我正在尝试从线程池中调用非静态成员 function。 The submit function tells me error: must use '.
提交 function 告诉我错误:必须使用 '. ' or '-> ' to call pointer-to-member function in 'f (...)', eg '(... ->* f) (...)' for the std::future<decltype(f(args...)) portion on the first line.
' 或 '-> ' 在 'f (...)' 中调用指向成员 function 的指针,例如 std::future<decltype( f(args...)) 部分在第一行。 I'm trying to not create a static function.
我试图不创建 static function。 I've tried a few combinations but I don't think I understand what it is asking for.
我尝试了一些组合,但我认为我不明白它的要求。 Would anyone help?
有人会帮忙吗?
auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))> {
// Create a function with bounded parameters ready to execute
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
// Encapsulate it into a shared ptr in order to be able to copy construct / assign
auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
// Wrap packaged task into void function
std::function<void()> wrapper_func = [task_ptr]()
{
(*task_ptr)();
};
// Enqueue generic wrapper function
m_queue.enqueue(wrapper_func);
// Wake up one thread if its waiting
m_conditional_lock.notify_one();
// Return future from promise
return task_ptr->get_future();
}
Update: I changed auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))>
to auto submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>
更新:我
changed auto submit(F&& f, Args&&... args) -> std::future<decltype(f(args...))>
更改为auto submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>>
Now I'm getting a new compiler error "no type named 'type'".现在我收到一个新的编译器错误“没有名为'type'的类型”。 Picture below.
下图。
To correctly get the result type of a call to a member function or a normal function you must use std::invoke_result_t
:要正确获取对成员 function 或普通 function 的调用的结果类型,您必须使用
std::invoke_result_t
:
auto submit(F&& f, Args&&... args) -> std::future<std::invoke_result_t<F, Args...>> {
// ...
}
That way, both member function and non member function will work.这样,成员 function 和非成员 function 都可以工作。
Consider that when sending a member function, you must also pass the instance:考虑发送成员 function 时,还必须传递实例:
// object of type MyClass -----v
submit(&MyClass::member_func, my_class, param1, param2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.