This question already has an answer here:
- using std::async with template functions 2 answers
Given the following MWE of a Module class usage, intended to offer a futured "reload" of an owned std::unique_ptr content
#include <future>
#include <memory>
#include <string>
/**
* C++11 lack
*/
template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&... params)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}
template <class T>
class Module
{
std::future<void> _futureReload;
std::unique_ptr<T> ptr;
/**
* time consuming
*/
template <typename... Ts>
void _future_reload(Ts&... params)
{
ptr = make_unique<T>(std::forward<Ts>(params)...);
}
public:
Module() :
ptr(nullptr)
{}
/**
* future triggering
*/
template <typename... Ts>
void reload(Ts&... params)
{
ptr.reset();
_futureReload = std::async(std::launch::async,
&Module::_future_reload, this,
std::forward<Ts>(params)...); // ERROR LINE
}
};
class Foo
{
public:
Foo(std::string s)
{}
};
int main()
{
Module<Foo> modFoo;
std::string sBAR("BAR");
modFoo.reload(sBAR);
}
A g++ --std=c++11 build fails on ERROR LINE line, complaining about
error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, Module<Foo>*, std::__cxx11::basic_string<char>)’
_futureReload = std::async(std::launch::async, &Module::_future_reload, this, std::forward<Ts>(params)...);
What I'm trying to achieve is some sort of triple perfect-forward of arguments, but am I missing something ?
Aucun commentaire:
Enregistrer un commentaire