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)
    {
        try
        {
            promise.set_value(func()); 
            // Note: Will not work with std::promise<void>. Needs some meta-template programming which is out of scope for this example.
        }
        catch(...)
        {
            promise.set_exception(std::current_exception());
        }
    }, std::move(promise))).detach();

    return future;
}

Feedback about page:

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


Futures and promises:
* Deferred async example

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
137 Futures and promises
139 Mutexes
142 decltype