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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With