C++,Linux:错误:从 ‘boost::unique_future’ 转换到非标量类型 ‘boost::shared_future’要求。如何绕过它?
我尝试使用 Boost 线程期货。如此处 我们可以得到共享未来 来自 打包任务。
所以我在linux上尝试这样的函数:
template <class task_return_t>
void pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
boost::shared_future<task_return_t> fi= pt->get_future(); // error
//...
但是调用它时出错:
../../src/cf-util/thread_pool.h: In member function ‘void thread_pool::pool_item(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’:
../../src/cf-util/thread_pool.h:64:3: instantiated from ‘void thread_pool::post(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’
../../src/cf-server/server.cpp:39:27: instantiated from here
../../src/cf-util/thread_pool.h:124:58: error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested
我之前没有从该任务中获取任何未来。 所有源代码,我拨打电话的地点, <一href="http://code.google.com/p/cloudobserver/source/browse/trunk/CloudServer/src/cf-util/thread_pool.h?r=1477" rel="nofollow">我的线程池是被称为。在 Visual Studio 2010 下的 Windows 上,它可以完美编译和运行。
我该怎么办?如何修复或解决此错误?
I try to work with boost thread futures. So as shown here we can get shared future from packaged task.
So I try such function on linux:
template <class task_return_t>
void pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
boost::shared_future<task_return_t> fi= pt->get_future(); // error
//...
but I get error calling it:
../../src/cf-util/thread_pool.h: In member function ‘void thread_pool::pool_item(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’:
../../src/cf-util/thread_pool.h:64:3: instantiated from ‘void thread_pool::post(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’
../../src/cf-server/server.cpp:39:27: instantiated from here
../../src/cf-util/thread_pool.h:124:58: error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested
I did not take any futures from that task before. all source code, place where I do call from, my thread pool that is being called. And on Windows under Visual Studio 2010 it compiles and works perfectly.
What shall I do? how to fix or get around this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从
unique_future
到shared_future
的转换使用 文档:这使用右值引用来确保它在您打算破坏“其他”值的源代码。 VC++2010 可能默认提供了足够的右值引用支持来直接支持,而您在 linux 上使用的编译器(可能是 gcc)需要一个额外的标志,例如
-std=gnu++0x
支持它。如果没有该标志,Boost 会退回到“移动模拟”,这可能(我没有检查过)需要您编写:The conversion from
unique_future
toshared_future
uses the following constructor in the documentation:This uses an rvalue reference to ensure that it's clear in the source code that you intended to destroy the value of 'other'. It's possible that VC++2010 provides enough rvalue reference support by default to support that directly, while the compiler you're using on linux (probably gcc) requires an extra flag like
-std=gnu++0x
to support it. Without that flag, boost falls back to "move emulation", which might (I haven't checked this) require you to write:杰弗里·亚斯金是正确的。该函数在 Linux 上使用 -std=gnu++0x 标志使用 g++ (4.6.3) 进行编译,如果您想要严格的 ISO 编译,则使用 -std=c++0x 标志进行编译。这些 -std 标志现已弃用,分别支持 gnu++11 和 c++11。 (我刚刚发表了评论,但还没有足够的权限)
Jeffrey Yasskin is correct. The function compiles with g++ (4.6.3) on Linux with the -std=gnu++0x flag, or with the -std=c++0x flag if you want strictly ISO compilation. These -std flags are now deprecated in favour of gnu++11 and c++11 respectively. (I'd have just made this a comment but don't yet have sufficient privelege)
您是否可能忘记包含正确的标题?
如果没有,像下面这样的不同构造函数可以工作吗?
Is it possible you forgot to include the right headers?
If not, does a different constructor like the one below work?
在 C++11 中,您应该调用 std::future::share() 从 future 获取共享未来。例如,
In C++11 you should call std::future::share() to obtain a shared_future from a future. E.g.,