在列表中存储未来 [英] Storing a future in a list

查看:71
本文介绍了在列表中存储未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将使用异步生成的多个线程的期货存储在列表中,以便以后检索它们的结果.

I want to store the futures of several threads spawned using async in a list to retrieve their results later.

future<int> f = async(doLater, parameter);
list<future<int>> l;
l.push_back(f);

但是编译器会显示以下错误消息

However the compiler prints the following error message

/usr/include/c ++/4.7/bits/stl_list.h:115:71:错误:使用已删除的函数'std :: future< _Res> :: future(const std :: future< _Res>& )[with _Res = int; std :: future< _Res> = std :: future]'

/usr/include/c++/4.7/bits/stl_list.h:115:71: error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = int; std::future<_Res> = std::future]'

我做错什么了吗?不是应该存储期货的清单吗?如果不是,那该怎么用呢?

Am i doing something wrong or aren't lists supposed to store futures? If they are not, what to use instead?

推荐答案

std::future是不可复制的-您需要进入列表.要么

std::future is not copyable - you need to move into the list. Either:

future<int> f = async(doLater, parameter);
list<future<int>> l;
l.push_back(std::move(f));

或:

list<future<int>> l;
l.push_back(async(doLater, parameter));

将起作用,后者是更可取的,因为它不会使移出的对象乱扔范围.

will work, with the latter being preferable since it doesn't leave a moved-from object littering the scope.

这篇关于在列表中存储未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆