Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::future .then() continuations returning boost::future

Tags:

I was reading the new C++ proposal regarding improvements to std::future and std::promise here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3857.pdf and it says

It is a common situation that the body of a then function object will itself be a future-based operation, which leads to the then() returning a future>. In this case, it is almost always the case that what you really care about is the inner future, so then() performs an implicit unwrap() (see below) before returning.

Therefore in the following code

auto promise = std::promise<int>{};
auto another_future = promise.get_future().then([](auto future) {
    return std::async([]() { return 1; });
});

the type of another_future is std::future<int> and not std::future<std::future<int>>

I was trying to use boost::future to achieve the same thing but it seems like boost continuations do not implicitly unwrap the future. Is there something I can do to enable the same behavior from boost futures? It seems like there is no unwrap() function available to use on the resulting feature either.

Am I stuck with having to unwrap the future via a constructor manually? Also on trying that I get the following error, what should I do?

inline explicit BOOST_THREAD_FUTURE(
    BOOST_THREAD_RV_REF(
        BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R> >) other); // EXTENSION
like image 525
Curious Avatar asked May 25 '17 00:05

Curious


1 Answers

Unwrapping is a conditional extension for Boost Thread:

#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#include <boost/thread.hpp>

int main() {
    auto promise = boost::promise<int>{};
    boost::future<int> another_future = 
        promise.get_future().then([](auto) {
            return boost::async([]() { return 1; });
        }).unwrap();
}
like image 121
sehe Avatar answered Sep 25 '22 09:09

sehe