Deferred async example

suggest change

This code implements a version of std::async, but it behaves as if async were always called with the deferred launch policy. This function also does not have async’s special future behavior; the returned future can be destroyed without ever acquiring its value.

template<typename F>
auto async_deferred(F&& func) -> std::future<decltype(func())>
    using result_type = decltype(func());

    auto promise = std::promise<result_type>();
    auto future  = promise.get_future();

    std::thread(std::bind([=](std::promise<result_type>& promise)
            // Note: Will not work with std::promise<void>. Needs some meta-template programming which is out of scope for this example.
    }, std::move(promise))).detach();

    return future;

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents