vector of future in c++11

11,898

Solution 1

Here is the test code presented using either of the two legal options:

#include <vector>
#include <future>
#include <iostream>
#include <algorithm>

using namespace std;

// option 1 : pass a reference to the future
void test1()
{
    vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
    auto K = [=](double z){
    double y=0; 
    for (const auto x : v)
        y += x*x*z;
    return y;
    };

    vector<future<double>> VF;
    for (double i : {1,2,3,4,5,6,7,8,9})
    VF.push_back(async(K,i));

    for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
}

// option 2 : store shared_futures which allow passing copies
void test2()
{
    vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
    auto K = [=](double z){
    double y=0; 
    for (const auto x : v)
        y += x*x*z;
    return y;
    };

    vector<shared_future<double>> VF;
    for (double i : {1,2,3,4,5,6,7,8,9})
    VF.push_back(async(K,i));

    for_each(VF.begin(), VF.end(), [](shared_future<double> x){cout << x.get() << " "; });
}

Solution 2

You can't copy futures.

Either use a reference, or store a shared_future.

Solution 3

Copy constructor of future is deleted, so you cannot copy them. Use reference:

for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
                                                ^~~~~ !
Share:
11,898
Giuseppe Levi
Author by

Giuseppe Levi

Updated on June 06, 2022

Comments

  • Giuseppe Levi
    Giuseppe Levi almost 2 years

    Hi I have created a vector of future in C++11 using a lambda function.

     vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
    auto K = [=](double z){
        double y=0; 
    for (const auto x : v)
        y += x*x*z;
    return y;
    };
    vector<future<double>> VF;
    for (double i : {1,2,3,4,5,6,7,8,9})
    VF.push_back(async(K,i));
    

    It worked successfully but when I tried to retrieve the values via a for_each call I obtained a compilation error that I do not understand.

     for_each(VF.begin(), VF.end(), [](future<double> x){cout << x.get() << " "; });
    

    The values were successfully obtained by an old style for loop:

     for (int i = 0; i < VF.size(); i++)
        cout << VF[i].get() << " ";
    

    Why I was not able to use the for_each function ? I was using Visual Studio 2013 trying also the INTEL ( V16) compiler.

  • Richard Hodges
    Richard Hodges about 8 years
    nicely stolen while I was preparing the demo code :-)
  • Lightness Races in Orbit
    Lightness Races in Orbit about 8 years
    @RichardHodges: I didn't "steal" it (N.B. community wiki) I simply moved it from the wrong place to the right place. If you want answer rules to apply to your post, write it as an answer!! & if you're not ready to post your answer yet then simply don't until you are. #FGITW
  • Lightness Races in Orbit
    Lightness Races in Orbit about 8 years
    Good stuff. But what's the answer/conclusion?
  • Richard Hodges
    Richard Hodges about 8 years
    @LightnessRacesinOrbit There are already some correct answers here. This is merely for completeness and exposition.
  • Giuseppe Levi
    Giuseppe Levi about 8 years
    First of all I would like to thank everybody who has answered. Second the conclusion is that in order to use a future inside an algorithms one should or pass a pointer or use the shared_future declaration.
  • Richard Hodges
    Richard Hodges about 8 years
    @GiuseppeLevi you mean a reference!
  • Giuseppe Levi
    Giuseppe Levi about 8 years
    Oh YES ! My Old Style Programming mind influence me! ( My first programming language was F77 in the '80 !) ... As a Reference see: cplusplus.com/articles/ENywvCM9