std :: move在成对存储的std :: future上 [英] std::move on a std::future stored in a pair
问题描述
我有一张地图:
typedef std::map<std::string, std::pair<std::vector<SomeObject>, std::future<boost::optional<uint64_t>>>> t_map;
t_map m;
当我插入地图时,会向该对添加默认构造的Future:
When I insert in the map, I add a default constructed future to the pair:
...
std::future<boost::optional<uint64_t>> fut;
m.insert(t_map::value_type(mystring, std::make_pair(myvector, fut)));
进一步,我进行了std :: async调用,返回了将来的对象,然后尝试移动
Further on, I make a std::async call returning the future object which I then try to move to the pair:
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));
我不确定此移动语义出了什么问题。
有任何建议吗?
I am not sure whats going wrong with this move semantics. Any advise?
更新
我尝试使用operator =正如Barry所建议的那样,但是我得到了与operator =不匹配的错误。
为什么左手操作数是const限定符?我本以为该呼叫将通过移动分配运算符解决:
I tried using operator= as Barry suggested, however I get the error no match to operator=. Why is the left hand operand being const qualified? I would have thought that the call will get resolved with the move assignment operator:
future& operator=(future&& __fut) noexcept
错误:
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> >*’
推荐答案
此:
m->second.second(std::move(f));
应为:
m->second.second = std::move(f);
按原样,您正在尝试调用 未来
对象。但是未来
是不可调用的,因此出现有关无法匹配到...的错误
As is, you're trying to call the future
object. But future
isn't callable, hence the error about "no match for call to ..."
这篇关于std :: move在成对存储的std :: future上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!