Como armazenar genéricas packaged_tasks em um contêiner?
Estou tentando fazer uma 'tarefa' no estilo destd::async
e guarde-o em um recipiente. Estou tendo que pular os bastidores para alcançá-lo, mas acho que deve haver uma maneira melhor.
std::vector<std::function<void()>> mTasks;
template<class F, class... Args>
std::future<typename std::result_of<typename std::decay<F>::type(typename std::decay<Args>::type...)>::type>
push(F&& f, Args&&... args)
{
auto func = std::make_shared<std::packaged_task<typename std::result_of<typename std::decay<F>::type(typename std::decay<Args>::type...)>::type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
auto future = func->get_future();
// for some reason I get a compilation error in clang if I get rid of the `=, ` in this capture:
mTasks.push_back([=, func = std::move(func)]{ (*func)(); });
return future;
}
Então eu estou usandobind
->packaged_task
->shared_ptr
->lambda
->function
. Como posso fazer isso melhor / de maneira mais otimizada? Certamente seria mais fácil se houvesse umastd::function
o que poderia levar uma tarefa não copiável, mas móvel. Posso std :: forward args na captura de um lambda, ou tenho que usarbind
?