我从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>。
我做错了什么?
备注: on VS2012,如果我使用std::async(func)而不是boost::async(func),它确实能按预期工作,未来的类型被推断为int。
发布于 2014-11-30 18:24:30
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的配置,boost::result_of<T>特性可能以以下两种方式之一工作:
decltype()。F中寻找嵌套的result<T>类型--或者在F中寻找嵌套的result<T>类模板(如果函子的参数数大于零,因为可能存在重载)。如果使用后一种方法(2),上述两种方法都不适用于lambda的类型,因此Boost的结果是默认假设推断void作为返回类型。
为了确保Boost实现将使用decltype()操作符(对lambda的调用表达式有效),您需要在Boost头包含之前添加一个定义:
#define BOOST_RESULT_OF_USE_DECLTYPE或将此定义添加到boost/config/user.hpp文件中。
https://stackoverflow.com/questions/27215376
复制相似问题