How to use boost::bind with non-copyable params, for example boost::promise?

不羁的心 提交于 2019-12-04 02:57:44

I'm not sure how to use a move constructor instead, but another approach is to use boost::ref that creates copyable references to objects, and you can then pass those into boost::bind.

I see that you uses std::move. Why don't you use std::bind which should be aware of move semantics?

template<class F, class... BoundArgs>
unspecified bind(F&&, BoundArgs&&...);
template<class R, class F, class... BoundArgs>
unspecified bind(F&&, BoundArgs&&...);

Wht about declaring a move version of fullfil_1

void fullfil_1(boost::promise<int>&é prom, int x)
{
  prom.set_value(x);
}

Boost.Bind doesn't supports move semantics yet (at least I'm not aware of). I hope that the currently reviewed Boost.Move will be accepted, and that Boost.Bind, Boost.Lambda and Boost.Phoenix will add the move semantics interfaces.

You can try composing ref and move as follows

boost::function<void()> f_set_one = boost::bind(&fullfil_1, boost::ref(std::move(pi)), 1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!