Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 637
Manuel Avatar asked Nov 21 '13 16:11

Manuel


1 Answers

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.

like image 151
Casey Avatar answered Oct 31 '22 21:10

Casey