gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:40 24 4
gpt4 key购买 nike

我尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com