boost :: async()中boost :: future <>的类型

阿迪·沙维特(Adi Shavit)

我从boost::async()(Boost 1.56,Windows:VS2010和VS2012)得到了意外的结果

#include <boost/thread/future.hpp>
...
auto func = [](){ return 123; };
auto boostFut = boost::async(func);
// boostFut = 42; // intentional error to reveal deduced type in compilation error

出于某种原因,boostFut推导出作为boost::unique_future<void>代替boost::unique_future<int>

我究竟做错了什么?

注意:在VS2012上,如果我使用VS2012std::async(func)而不是boost::async(func)按预期工作,则将来的类型推导为int

皮特·斯科特尼克

boost::async需要确定参数函子调用的结果类型。为了做到这一点,Boost使用自己的boost::result_of<T>类模板实现。也就是说,async声明如下所示:

template <class F>
boost::future<typename boost::result_of<typename boost::decay<F>::type()>::type>
async(F f);

根据编译器的功能/增强的配置,boost::result_of<T>特质可能以两种方式之一起作用:

  1. 使用decltype()来自呼叫表达。
  2. 在其中查找嵌套的result_typetypedefF或在其中查找嵌套的result<T>类模板F(如果函子的参数数量大于零,因为可能存在重载)。

如果使用后一种方法(2),则上述两种方法均不适用于lambda的类型,因此Boost最终会推导出 默认假设,void以作为返回类型。

为了确保您的Boost实现将使用decltype()运算符(可以很好地使用lambda的调用表达式),您需要在包含Boost标头之前预先定义一个定义:

#define BOOST_RESULT_OF_USE_DECLTYPE

或将此定义添加到boost/config/user.hpp文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

boost :: future .then()继续返回boost :: future

boost :: future :: then如何实现?

boost :: future无法编译

使用boost :: future :: then在主线程中执行

boost async_accept无法与boost asio use_future选项一起使用

boost变体返回类型

使用boost :: future和continuation和boost :: when_all

你如何在 boost::python 中“从 __future__ 导入部门”?

boost :: future和std :: future的不同行为

错误:“ boost :: array”不是类型

boost :: optional和类型转换

从istream读取boost :: variant类型

如何返回由boost :: varaint返回类型中包含的类型的子集制成的boost :: variant

在boost :: asio :: async_connect上等待超时失败(std :: future :: wait_for)

使用use_future的Boost Asio async_send_to多播不起作用

在boost MPL序列中搜索带有boost占位符的类型

在'struct boost :: enable_if <boost :: is_pod <T>,void>'中没有名为'type'的类型

通过boost :: mpl获得boost :: variant的类型索引

使用use_future的多播Boost Asio async_recieve_与async_send_to不能同时使用

如何使用boost :: mutex作为std :: map中的映射类型?

从 boost fusion 适配结构中获取成员类型列表

在命名空间 boost 中没有名为 asio 的类型

如何在Boost Graph库中获取属性的类型

使用访客无法在Boost Variant中对类型进行分类

如何从Boost属性树中读取字段类型

boost :: variant仅移动类型的处理中的奇怪行为

如何在Boost Beast WebSocket中传递模型类型

“Future<GeoFirePoint>”不是类型转换中“GeoFirePoint”类型的子类型

boost.future:这两段代码是否等效?