Search code examples
c++templatesstdthread

thread failed to dedact callback function


I have the following function:

    template <class T>
    void programManager(std::shared_ptr<BaseStrategy<T>> st,std::shared_ptr<SMDSSubscriber<T>> md){some code}

and it the callable function for std::thread

bac declare as:

std::shared_ptr<BaseStrategy<EZXOrderEventHandler>> bac = make_shared<BacStrategy<EZXOrderEventHandler>>

and md declare as:

std::shared_ptr<SMDSSubscriber<EZXOrderEventHandler>> dataMarket = make_shared<SMDSSubscriber<EZXOrderEventHandler>>();
   std::thread t2(&programManager,bac,dataMarket);

and I'm getting the error:

error: no matching function for call to ‘std::thread::thread(, std::shared_ptr >&, std::shared_ptr >&)’ std::thread t2(&programManager,bac,dataMarket);

note: candidate: template std::thread::thread(_Callable&&, _Args&& ...) thread(_Callable&& __f, _Args&&... __args)

template argument deduction/substitution failed:

/home/yaodav/Desktop/git_repo/test/main.cpp:347:54: note: couldn't deduce template parameter ‘_Callable’

     std::thread t2(&programManager,bac,dataMarket);

Solution

  • std::thread::thread requires, as its first argument, a callable object. programManager itself is not a callable object, it's a function template, which is basically just a generic prescription how its instances should be generated.

    In your case, you need to pass a corresponding instance of programManager as an argument, which seems to be programManager<EZXOrderEventHandler>.