Storing a future in a list

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?


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.

链接地址: http://www.djcxy.com/p/30744.html

上一篇: std ::异步问题取决于Android版本

下一篇: 将未来存储在列表中