samedi 26 janvier 2019

error: no matching constructor for initialization of 'std::thread' issue in forwarding function pointer to thread

I am facing an issue in forwarding a callable object (function pointer) to a thread. Here is an example code trying achieve.

#include <iostream>
#include <thread>                   

using namespace std;

void my_func(int x, int y, int z){
    cout << "X: " << x << "\tY: " << y << "\tZ "<<z << endl;
}

template <typename T, typename ...  Args>
void my_thread(T func, Args&&... args){
    // Do something
    func(forward<Args&&>(args)...);
}

template <typename T, typename ...  Args>
void call_thread(T func,  Args&& ... args){
     // Do something
    thread t1(my_thread, func, forward<Args&&>(args)...);
    t1.detach();
}

int main()
{
    call_thread(my_func, 2,5,6);
    return 0;
}

Error:

In instantiation of 'void call_thread(T, Args&& ...) [with T = void (*)(int, int, int); Args = {int, int, int}]':
required from here

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, void (*&)(int, int, int), int, int, int)'
    thread t1(my_thread, func, forward<Args&&>(args)...);
                                                       ^

Aucun commentaire:

Enregistrer un commentaire