我正在阅读关于改进 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/