Boost logo

Boost :

Subject: Re: [boost] Common future base class (was Re: Boost.Fibermini-review September 4-13)
From: TONGARI J (tongari95_at_[hidden])
Date: 2015-09-06 22:20:24


2015-09-07 9:30 GMT+08:00 Agustín K-ballo Bergé <kaballo86_at_[hidden]>:

> On 9/6/2015 6:26 PM, Giovanni Piero Deretta wrote:
>
>> BTW, it is sort of possible (but quite inefficient) to attach a
>> continuation to an unique future without consuming it:
>>
>> template<class T>
>> future<void> when_ready(std::future<T>& x)
>> {
>> auto shared_x = x.share();
>> x = shared_x.then([](auto&&x) { return x.get(); }
>>
>
> No, this ^ introduces a copyable requirement.

Assuming the standard provides await_ready/await_suspend/await_resume as
customization points as suggested by Eric, there's a simple and universal
solution, :

```c++
template<class Task>
struct ready_awaiter
{
    Task task;

    bool await_ready()
    {
        return std::await_ready(task);
    }

    template<class F>
    auto await_suspend(F&& f)
    {
        return std::await_suspend(task, std::forward<F>(f));
    }

    void await_resume() const noexcept {}
};

template<class Task>
inline ready_awaiter<Task> ready(Task&& task)
{
    return {std::forward<Task>(task)};
}
```

By ignoring `await_resume`, `await ready(x)` won't consume `x`.

Unfortunately, these customization points aren't available to the user in
the current proposal, while I did implement this and it's called `awaken`
in my emulation library:
https://github.com/jamboree/co2/blob/master/include/co2/coroutine.hpp#L584


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk