使用 boost::bind 和 boost::lambda::new_ptr 返回一个shared_ptr构造函数

发布于 2024-09-18 10:23:43 字数 1286 浏览 3 评论 0 原文

给定一个 A 类,

class A {
 public:
  A(B&) {}
};

我需要一个 boost::function(B&)> 对象。

我不想创建一个临时函数

boost::shared_ptr<A> foo(B& b) {
  return boost::shared_ptr<A>(new A(b));
}

来解决我的问题,我正在尝试通过绑定 lambda::new_ptr 来解决它。

boost::function<boost::shared_ptr<A> (B&)> myFun
= boost::bind(
    boost::type<boost::shared_ptr<A> >(),
    boost::lambda::constructor<boost::shared_ptr<A> >(),
    boost::bind(
      boost::type<A*>(),
      boost::lambda::new_ptr<A>(),
      _1));

也就是说,我分两步绑定了A的new_ptr和shared_ptr的构造函数。显然它不起作用:

/usr/include/boost/bind/bind.hpp:236: error: no match for call to ‘(boost::lambda::constructor<boost::shared_ptr<A> >) (A*)’
/usr/include/boost/lambda/construct.hpp:28: note: candidates are: T boost::lambda::constructor<T>::operator()() const [with T = boost::shared_ptr<A>]
/usr/include/boost/lambda/construct.hpp:33: note:                 T boost::lambda::constructor<T>::operator()(A1&) const [with A1 = A*, T = boost::shared_ptr<A>]

我应该如何进行绑定? 提前致谢, 弗朗西斯科

Given a class A,

class A {
 public:
  A(B&) {}
};

I need a boost::function<boost::shared_ptr<A>(B&)> object.

I prefer not to create an ad-hoc function

boost::shared_ptr<A> foo(B& b) {
  return boost::shared_ptr<A>(new A(b));
}

to solve my problem, and I'm trying to solve it binding lambda::new_ptr.

boost::function<boost::shared_ptr<A> (B&)> myFun
= boost::bind(
    boost::type<boost::shared_ptr<A> >(),
    boost::lambda::constructor<boost::shared_ptr<A> >(),
    boost::bind(
      boost::type<A*>(),
      boost::lambda::new_ptr<A>(),
      _1));

that is, I bind in two steps the new_ptr of a A and the constructor of shared_ptr. Obviously it doesn't work:

/usr/include/boost/bind/bind.hpp:236: error: no match for call to ‘(boost::lambda::constructor<boost::shared_ptr<A> >) (A*)’
/usr/include/boost/lambda/construct.hpp:28: note: candidates are: T boost::lambda::constructor<T>::operator()() const [with T = boost::shared_ptr<A>]
/usr/include/boost/lambda/construct.hpp:33: note:                 T boost::lambda::constructor<T>::operator()(A1&) const [with A1 = A*, T = boost::shared_ptr<A>]

How should I do the binding instead?
Thanks in advance,
Francesco

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

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

发布评论

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

评论(1

挽袖吟 2024-09-25 10:23:43

使用 boost::lambda::bind 而不是 boost::bind

#include <boost/shared_ptr.hpp>
#include <boost/lambda/bind.hpp> // !
#include <boost/lambda/construct.hpp>
#include <boost/function.hpp>

void test()
{
  using namespace boost::lambda;
  boost::function<boost::shared_ptr<A>(B&)> func = 
    bind( constructor< boost::shared_ptr<A> >(), bind( new_ptr<A>(), _1 ) );
}

Use boost::lambda::bind instead of boost::bind.

#include <boost/shared_ptr.hpp>
#include <boost/lambda/bind.hpp> // !
#include <boost/lambda/construct.hpp>
#include <boost/function.hpp>

void test()
{
  using namespace boost::lambda;
  boost::function<boost::shared_ptr<A>(B&)> func = 
    bind( constructor< boost::shared_ptr<A> >(), bind( new_ptr<A>(), _1 ) );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文