Storing a future in a list

荒凉一梦 提交于 2019-12-12 07:49:42

问题


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: 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?


回答1:


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));

or:

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.



来源:https://stackoverflow.com/questions/20126551/storing-a-future-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!