std :: move on std :: future存储在一对中

时间:2016-05-20 12:26:54

标签: c++ c++11

我有一张地图:

    typedef std::map<std::string, std::pair<std::vector<SomeObject>, std::future<boost::optional<uint64_t>>>> t_map;
    t_map m;

当我在地图中插入时,我会在对中添加默认构造的未来:

...
std::future<boost::optional<uint64_t>> fut;
m.insert(t_map::value_type(mystring, std::make_pair(myvector, fut)));

接下来,我创建了一个std :: async调用,返回未来的对象,然后我尝试移动到该对:

auto record = m.find(somestring);
if (record != m.end())
{ 
    std::future<boost::optional<uint64_t>> f = std::async(fn, args);
    m->second.second(std::move(f));
}

然而,我收到错误:

error: no match for call to ‘(const std::future<boost::optional<long unsigned int> >) (std::remove_reference<std::future<boost::optional<long unsigned int> >&>::type)’
                         m->second.second(std::move(f));

我不确定这种移动语义会出错。 有什么建议吗?

更新

我尝试使用operator =正如Barry建议的那样,但是我得到的错误与operator =不匹配。     为什么左手操作数是const限定的?我原本以为调用将通过移动赋值运算符解决:

future& operator=(future&& __fut) noexcept

ERROR:

error: no match for ‘operator=’ (operand types are ‘const std::future<boost::optional<long unsigned int> >’ and ‘std::remove_reference<std::future<boost::optional<long unsigned int> >&>::type {aka std::future<boost::optional<long unsigned int> >}’)
                         m->second.second = std::move(f);
                                                 ^
note: candidates are:
In file included from test.cpp:20:0:
/.../linux/x86-64/release/opt/gcc-4.9.1/include/c++/4.9.1/future:687:15: note: std::future<_Res>& std::future<_Res>::operator=(const std::future<_Res>&) [with _Res = boost::optional<long unsigned int>] <near match>
       future& operator=(const future&) = delete;
               ^
note:   no known conversion for implicit ‘this’ parameter from ‘const std::future<boost::optional<long unsigned int> >*’ to ‘std::future<boost::optional<long unsigned int> >*’
/.../linux/x86-64/release/opt/gcc-4.9.1/include/c++/4.9.1/future:689:15: note: std::future<_Res>& std::future<_Res>::operator=(std::future<_Res>&&) [with _Res = boost::optional<long unsigned int>] <near match>
       future& operator=(future&& __fut) noexcept
               ^
note:   no known conversion for implicit ‘this’ parameter from ‘const std::future<boost::optional<long unsigned int> >*’ to ‘std::future<boost::optional<long unsigned int> >*’

1 个答案:

答案 0 :(得分:4)

此:

m->second.second(std::move(f));

应该是:

m->second.second = std::move(f);

按原样,您正在尝试调用 future个对象。但是future不可调用,因此错误“不匹配调用...”