简体   繁体   English

没有匹配的 function 来调用:错误:必须使用 '.*' 或 '->*' 来调用指向成员 function 在 'f (...)' 中的指针,例如 '(... ->* f) (...)'

[英]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();
    }

Compiler Output编译器 Output

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.下图。

no type named 'type' compiler error没有名为“类型”的类型编译器错误

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.

相关问题 必须使用'。*'或' - > *'在'lessThan(...)'中调用指向成员的函数,例如'(... - > * lessThan)(...)' - must use '.*' or '->*' to call pointer-to-member function in 'lessThan (…)', e.g. '(… ->* lessThan) (…)' c ++中的函数指针:错误:必须使用&#39;。*&#39;或&#39; - &gt; *&#39;来调用函数中的指向成员函数 - function pointers in c++ : error: must use '.*' or '->*' to call pointer-to-member function in function 在调用传递给模板函数的函数时调用指针成员函数 - Calling pointer-to-member function in call for a function passed to a template function 调用不是内联的常量指针到成员函数 - Call to constant pointer-to-member function not being inlined 如何通过保存在容器中的成员指针 function 调用? - How to call through pointer-to-member function saved in a container? 模板化的成员函数调用g ++错误:没有匹配的调用函数 - Templated member function call g++ error : no matching function for call 没有匹配的 function 调用 'normalize(cv::Point3f&amp;)' 错误 - no matching function for call to ‘normalize(cv::Point3f&)’ error 当我使用指向成员的指针调用成员函数时,分段错误 - segmentation fault when i call member function using pointer-to-member 指针到成员函数解析 - Pointer-to-member function resolution C ++通过指向基类的派生类中的指向成员函数调用 - C++ call via pointer-to-member function in a derived class from the base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM