boost的类型::future<> from boost::async()
Type of boost::future<> from boost::async()
我从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上,如果我使用std::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>
特征可能以以下两种方式之一工作:
- 使用调用表达式中的
decltype()
。
在 F
中查找嵌套的result_type
typedef 或在F
中查找嵌套的result<T>
类模板(如果函子的参数数大于零,因为可能存在重载(。
如果使用后一种方法 (2(,则上述替代方案都不适用于 lambda 的类型,因此 Boost 最终会默认假设推断void
为返回类型。
为了确保您的 Boost 实现将使用decltype()
运算符(适用于 lambda 的调用表达式(,您需要在包含 Boost 标头之前附加一个定义:
#define BOOST_RESULT_OF_USE_DECLTYPE
或将此定义添加到boost/config/user.hpp
文件中。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 为什么使用线程池时嵌套调用 boost::async 会阻塞?
- TCP中的Boost async处理程序
- boost :: async-不确定实现
- 如何将函数参数传递给boost::async()
- boost::asio async condition
- boost的类型::future<> from boost::async()
- 找不到究竟是什么 boost::async
- Boost equivalent of std::async()
- 如何在async boost::asio处理程序中获取接收消息的端点
- Boost async函数编译错误