2009/4/16 Igor R <boost.lists@gmail.com>
Hello,

Is it possible somehow to make a binder that stores "unexpected" argument?

Motivation: I have to create & pass a callback handler from within
intusively ref-counted object (MS-ATL) to an outer asynchronous
subsystem. If the object were shared-ptr enabled, I'd do this as
usually:

ThisObj::handler()
{
 //...
}
ThisObj::doSomething()
{
 asyncSubsystem_->doSomething(&ThisObj::handler, shared_from_this());
}

However, in my case I have to do something like this:

asyncSubsystem_->doSomething(&ThisObj::handler, this, mySmartPtr); //
store someOtherSmartPtr in the functor, just to ensure "this" won't
die before the handler is invoked

Is it possible with bind, lambda or some other library?

Thanks.

You can overload boost::get_pointer for your smart pointer.

  namespace boost {
  template <class T> T * get_pointer(CComPtr<T> const& p)
  {
    return p;
  }
  }

  CComPtr<ThisObj> self(this);
  asyncSubsystem_->doSomething(bind(&ThisObj::handler, self));

If you don't want or can't overload get_pointer, you can use double bind trick.

  CComPtr<ThisObj> self(this);
  asyncSubsystem_->doSomething(bind(bind(&ThisObj::handler, this), self));

First bind creates a function object that accepts any number of arguments, and second bind creates a function object that stores smart pointer inside of it.

Roman Perepelitsa.