lundi 23 octobre 2017

C++11 pass a member function to a thread using a proxy

I try to generate a general way to create threads that have an argunment a class method. But I am not able to compile succesfully the code I use the the following code

#include <iostream>
#include <thread>
#include <functional>
using namespace std;

class hello{
public:
    void f(){
        cout<<"f"<<endl;
    }
    virtual void ff(){
        cout<<"ff"<<endl;
    }
};

template <typename T, T> struct proxy;

template <typename T, typename R, typename ...Args, R (T::*mf)(Args...)>
struct proxy<R (T::*)(Args...), mf>
{
    static R call(T & obj, Args &&... args)
    {  
    //    function func = T::*mf;
        thread t(&T::*mf, &obj);
        return (obj.*mf)(std::forward<Args>(args)...);
    }
};
int main(){
    hello obj;
   typedef proxy<void(hello::*)(), &hello::f> hello_proxy;
   hello_proxy::call(obj);
}

When compile the following error is generated In static member function 'static R proxy::call(T&, Args&& ...)': 24:22: error: expected unqualified-id before '*' token

Aucun commentaire:

Enregistrer un commentaire