如何将 boost::bind 与不可复制的参数一起使用,例如 boost::promise?

发布于 2024-09-01 18:00:38 字数 1042 浏览 3 评论 0原文

某些 C++ 对象没有复制构造函数,但有移动构造函数。 例如,boost::promise。 如何使用它们的移动构造函数绑定这些对象?

#include <boost/thread.hpp>

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

boost::function<void()> get_functor() 
{
  // boost::promise is not copyable, but movable
  boost::promise<int> pi;

  // compilation error
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1);

  // compilation error as well
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1);

  // PS. I know, it is possible to bind a pointer to the object instead of 
  // the object  itself. But it is weird solution, in this case I will have
  // to take cake about lifetime of the object instead of delegating that to
  // boost::bind (by moving object into boost::function object)
  //
  // weird: pi will be destroyed on leaving the scope
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, boost::ref(pi), 1);
  return f_set_one;
}

Some C++ objects have no copy constructor, but have move constructor.
For example, boost::promise.
How can I bind those objects using their move constructors ?

#include <boost/thread.hpp>

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

boost::function<void()> get_functor() 
{
  // boost::promise is not copyable, but movable
  boost::promise<int> pi;

  // compilation error
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1);

  // compilation error as well
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1);

  // PS. I know, it is possible to bind a pointer to the object instead of 
  // the object  itself. But it is weird solution, in this case I will have
  // to take cake about lifetime of the object instead of delegating that to
  // boost::bind (by moving object into boost::function object)
  //
  // weird: pi will be destroyed on leaving the scope
  boost::function<void()> f_set_one = boost::bind(&fullfil_1, boost::ref(pi), 1);
  return f_set_one;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-09-08 18:00:38

我不确定如何使用移动构造函数,但另一种方法是使用 boost::ref 创建对对象的可复制引用,然后您可以将它们传递给 boost::bind。

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.

陌伤ぢ 2024-09-08 18:00:38

我看到你使用 std::move 。为什么不使用应该了解移动语义的 std::bind ?

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

关于声明 fullfil_1

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

Boost.Bind 的移动版本,Boost.Bind 还不支持移动语义(至少我不知道)。我希望目前审查的Boost.Move能够被接受,并且Boost.Bind、Boost.Lambda和Boost.Phoenix将添加移动语义接口。

您可以尝试按如下方式编写 ref 和 move

boost::function<void()> f_set_one = boost::bind(&fullfil_1, boost::ref(std::move(pi)), 1);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文