c++ - boost::future .then() 继续返回 boost::future

标签 c++ c++11 asynchronous boost future

我正在阅读关于改进 std::future 的新 C++ 提案和 std::promise这里http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3857.pdf它说

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.

因此在下面的代码中

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

another_future 的类型是std::future<int>而不是 std::future<std::future<int>>

我正在尝试使用 boost::future实现同样的目标,但似乎 boost continuations 并没有隐含地展开 future 。我可以做些什么来使 boost futures 具有相同的行为吗?好像没有unwrap()可用于结果特征的函数。

我是否坚持必须通过构造函数手动解包 future ?同样在尝试我得到以下错误时,我该怎么办?

inline explicit BOOST_THREAD_FUTURE(
    BOOST_THREAD_RV_REF(
        BOOST_THREAD_FUTURE<BOOST_THREAD_FUTURE<R> >) other); // EXTENSION

最佳答案

Unwrapping 是 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();
}

关于c++ - boost::future .then() 继续返回 boost::future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170631/

相关文章:

c++ - EXPECT_THROW - 实际 : it throws a different type, 谷歌测试

sockets - 单线程 NGINX 如何处理如此多的连接?

javascript - for 循环内的 async.waterfall 逃脱了 for 循环

c++ - iPhone OpenGL ES 不正确的 alpha 混合

c++ - 指向 std::vector 的指针,指针声明

c++ - 为什么带有 Bool 参数的函数接受 String?

c# - 为什么异步 LINQ Select lambda 不需要返回值

c++ - `std::string` 是否在内部将其字符存储为签名字符?

c++ - 什么是规范地用来提及引用合并的词

python - c++ 中的循环类似于 python(基于范围的 for)