C++,Linux:错误:从 ‘boost::unique_future’ 转换到非标量类型 ‘boost::shared_future’要求。如何绕过它?

发布于 2024-12-02 02:59:33 字数 1993 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归属感 2024-12-09 02:59:33

unique_futureshared_future 的转换使用 文档

shared_future(unique_future<R> && other);

这使用右值引用来确保它在您打算破坏“其他”值的源代码。 VC++2010 可能默认提供了足够的右值引用支持来直接支持,而您在 linux 上使用的编译器(可能是 gcc)需要一个额外的标志,例如 -std=gnu++0x 支持它。如果没有该标志,Boost 会退回到“移动模拟”,这可能(我没有检查过)需要您编写:

boost::shared_future<task_return_t> fi = boost::move(pt->get_future());

The conversion from unique_future to shared_future uses the following constructor in the documentation:

shared_future(unique_future<R> && other);

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:

boost::shared_future<task_return_t> fi = boost::move(pt->get_future());
吹泡泡o 2024-12-09 02:59:33

杰弗里·亚斯金是正确的。该函数在 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)

黑凤梨 2024-12-09 02:59:33

您是否可能忘记包含正确的标题?

如果没有,像下面这样的不同构造函数可以工作吗?

boost::shared_future<task_return_t> fi(pt->get_future());

Is it possible you forgot to include the right headers?

If not, does a different constructor like the one below work?

boost::shared_future<task_return_t> fi(pt->get_future());
埋葬我深情 2024-12-09 02:59:33

在 C++11 中,您应该调用 std::future::share() 从 future 获取共享未来。例如,

auto sf=std::async([]{/* something */}).share();

In C++11 you should call std::future::share() to obtain a shared_future from a future. E.g.,

auto sf=std::async([]{/* something */}).share();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文