【发布时间】:2014-08-07 09:01:49
【问题描述】:
我在 Boost.Thread 的 future 方面遇到了问题:除了原始类型之外,我似乎无法将任何东西放入 promise、future 或打包任务中。
这是一个最小的测试用例:
#include <boost/thread/future.hpp>
struct foo
{
foo(int i_): i(i_) {}
int i;
};
int main()
{
// A const future isn't much use, but I needed to prove
// the problem wasn't me trying to copy a unique_future
const boost::unique_future<foo>& fut = boost::make_ready_future( foo(42) );
}
在包含 Boost.Future 标头之前定义了 BOOST_THREAD_USES_MOVE,我在 gcc 4.8.2 和 Boost 1.55(完整输出 here)中收到以下错误:
../../deps/boost/include/boost/thread/future.hpp:3634:5: error: no matching function for call to ‘boost::promise<foo>::set_value(const foo&)’
p.set_value(boost::forward<future_value_type>(value));
^
似乎没有采用 const 左值引用的 promise::set_value() 重载。查看future.hpp 中的promise 和future_traits 似乎只有在未定义BOOST_NO_CXX11_RVALUE_REFERENCES 时才会存在const lvalue ref 重载。然而,这对我来说毫无意义......当没有右值引用时,肯定需要 const lvalue ref 重载吗? (请注意,即使我将可变左值 ref 传递给 make_ready_future(),也会发生这种情况。
如果我没有定义BOOST_THREAD_USES_MOVE,编译失败并出现以下错误(完整输出here):
../../deps/boost/include/boost/thread/detail/move.hpp:183:54: error: no matching function for call to boost::unique_future<foo>::unique_future(boost::unique_future<foo>)’
#define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE.move()
^
我错过了什么吗?
【问题讨论】:
-
保持#define BOOST_THREAD_USES_MOVE。并将其编译为: g++ boosttest.cpp -I boost_1_55/include -std=c++11 -L ./boost_1_55/lib/ -lboost_thread -lboost_system -lpthread
-
呵呵,刚刚意识到如果我们使用 -std=c++11 BOOST_THREAD_USES_MOVE 并不重要
-
感谢您的建议,但不幸的是我无法使用 C++11 :-(
-
当没有右值引用时,肯定需要 const lvalue ref 重载吗? 它总是需要的,它是
std::promise的标准化 API 的一部分,而你不需要t 只希望能够使用右值做出承诺。
标签: c++ boost compiler-errors