C++11 如何编写。然后使用std::future
我有一个简单的任务类:C++11 如何编写。然后使用std::future,c++11,C++11,我有一个简单的任务类: template<typename TType> class YHMTask { public: YHMTask() {}; template<typename TLambdaFun> auto then(TLambdaFun f) -> std::future<decltype(f(mTask.get()))> { std::move(mTask); return
template<typename TType>
class YHMTask
{
public:
YHMTask() {};
template<typename TLambdaFun>
auto then(TLambdaFun f) -> std::future<decltype(f(mTask.get()))>
{
std::move(mTask);
return std::async(std::launch::async, f, mTask.get());
}
std::future<TType> mTask;
private:
};
编译器给我这个错误:
error C2672: 'YHMTask<std::wstring>::then': no matching overloaded function found
error C2893: Failed to specialize function template 'YHMTask<unknown-type> YHMTask<std::wstring>::then(TLambdaFun)'
错误C2672:'YHMTask::then':未找到匹配的重载函数
错误C2893:无法专门化函数模板“YHMTask YHMTask::then(TLambdaFun)”
我该怎么做?当您创建
yhmTask
时,您正在移动此
,它是一个指针。YHMTask
的构造函数没有指向YHMTask
的指针,因此模板专门化失败。在移动它之前,您应该取消对该的引用:
YHMTask<decltype(f())> yhmTask(std::move(*this));
YHMTask YHMTask(std::move(*this));
听起来您对扩展std::future to have.then()感兴趣。我建议看一看。它能做你想做的事情,还有更多 允许您使用以下命令链接任务序列:
Future<double> fut =
fooFuture(input)
.then(futureA)
.then(futureB)
.then(futureC)
.then(d)
.then([](OutputD outputD) { // lambdas are ok too
return outputD * M_PI;
});
未来未来=
未来(输入)
.然后(未来)
.然后(未来b)
.然后(未来)
.然后(d)
。然后([](OutputD OutputD){//lambda也可以
返回输出d*M_PI;
});
还提供其他功能,如收集、映射和减少。吉特回购。看起来可能还支持.then构造 在这段代码和你昨天的问题之间,我怀疑你需要得到一个答案。我添加了一些代码,试图实现这一功能,但没有成功。你能帮我吗?好奇你能不能看看我在回答中提到的facebook图书馆?对你有用吗?
YHMTask<decltype(f())> yhmTask(std::move(*this));
Future<double> fut =
fooFuture(input)
.then(futureA)
.then(futureB)
.then(futureC)
.then(d)
.then([](OutputD outputD) { // lambdas are ok too
return outputD * M_PI;
});