BenK
BenK

Reputation: 333

async returns future of void type instead of expected type

I'm learning C++'s futures and asynchronous programming. I have the following code based on that in chapter 42 of Stroustrup's C++ reference. Both GCC and Microsoft compilers complain that there is no conversion from future<void> to future<InputIterator>in the call to push_back. Why is the call to async returning a future<void> instead of a future<std::iterator> that I would expect from std::find?

template<typename T, typename InputIter>
InputIter p_find(InputIter first, InputIter last, const T& value, const int grain)
{
    std::vector<std::future<InputIter>> results;
    while (first != last)
    {
        results.push_back(std::async([=]() mutable {std::find(first, first + grain, value); }));
        first += grain;
    }
    //blah blah

Upvotes: 0

Views: 680

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

Your lambda returns nothing (that is void), so the result of async() is std::future<void>. What you want is probably

results.push_back(std::async([=]() mutable {return std::find(first, first + grain, value); }));
//                                          ^^^^^^

Upvotes: 2

Related Questions