C++, Linux : error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested. 如何绕过它?

标签 c++ boost scheduled-tasks boost-thread future

我尝试使用 boost thread futures .所以如图here我们可以得到 shared future来自 packaged task .

所以我在 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

我之前没有从该任务中获取任何 future 。 all source code , place where I do call from , my thread pool that is being called .在 Visual Studio 2010 下的 Windows 上,它可以完美编译和运行。

我该怎么办?如何修复或绕过此错误?

最佳答案

unique_futureshared_future 的转换在the documentation 中使用了以下构造函数:

shared_future(unique_future<R> && other);

这使用右值引用来确保在源代码中清楚地表明您打算破坏“other”的值。 VC++2010 可能默认提供足够的右值引用支持以直接支持它,而您在 linux 上使用的编译器(可能是 gcc)需要一个额外的标志,如 -std=gnu++0x 支持它。如果没有那个标志,boost 会退回到“移动仿真”,这可能(我还没有检查过)需要你写:

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

关于C++, Linux : error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested. 如何绕过它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251538/

相关文章:

C++ STL for_each() 和仿函数,它在哪里创建新对象

c++ - 为什么 lambda 中的变量捕获如此复杂?

c++ - 如何将数据写入 MutableBufferSequence

asp.net - Godaddy 上的 ASMX 任务计划程序

c++ - gcc 编译/链接后的 .h~ 文件是什么?

linux - 如何使用 gcc 的自定义位置在 Linux 上安装 boost?

c++ - 如何使用 boost::units 添加你自己的基本单位和转换

windows - 使用 Packer powershell 配置程序将 Azure 文件共享映射到服务帐户

java - 在java线程中每隔一小时运行一次作业

c++ - 选择从 Windows 将文件拖放到 GLFW 应用程序中