3

クラスAを考えると、

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

boost::function<boost::shared_ptr<A>(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 のコンストラクターを 2 つのステップでバインドします。明らかに機能しません:

/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>]

代わりにバインディングを行うにはどうすればよいですか? 前もってありがとう、フランチェスコ

4

1 に答える 1

3

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 ) );
}
于 2010-09-06T18:58:15.747 に答える