Question

In C++17, there's a nice future like C#'s await.

std::future<int> get_answer()
{
    return std::async(std::launch::async, [] { return 42; });
}
std::future<void> do_something() resumable
{
    // ...
    int a = await get_answer();
    // ...
}

But I wonder whether boost::future is also can used or not:

boost::future<int> get_answer()
{
    return boost::async(boost::launch::async, [] { return 42; });
}
boost::future<void> do_something() resumable
{
    // ...
    int a = await get_answer();
    // ...
}
Was it helpful?

Solution

The paper that you linked (N3722) explicitly says that only std::future<T> and std::shared_future<T> are accepted as a return type for a resumable function:

The return type of a resumable function must be future or shared_future . The restrictions on T are defined by std::future, not this proposal, but T must be a copyable or movable type, or ‘void.’ It must also be possible to construct a variable of T without an argument; that is, it has to have an accessible (implicit or explic it) default constructor if it is of a class type.

However, the section 4 of the proposal (Generalization) proposes to lift the return type restriction. The returned type should be any type with the following restrictions:

The operand of the unary operator await can be any type S<<T>> (“S eventually holding a T”) with meets the following conditions:

  1. S has a parameter-less function get which will either eventually produce a value of type T, or throw an exception.
  2. S has a function then accepting a single-parameter function object taking a parameter S<<T>>, S<<T>>&, or const S<<T>>. Once passed to then, the value held by the parameter must be immediately available for retrieval by a call to get.
  3. Optionally, if S<<T>> has a bool-returning function is_ready() indicating whether a value is already held, the implementation of await can be made more efficient.

Currently, the discussion is still open. It the generalization is accepted, a resumable function will be able to return a boost::future. Otherwise, it will be limited to std::future and std::shared_future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top