Boost logo

Boost Users :

Subject: Re: [Boost-users] Delegating to boost::function with arbitrary signature
From: TONGARI J (tongari95_at_[hidden])
Date: 2013-12-16 10:31:04


2013/12/16 Alexander Lamaison <awl03_at_[hidden]>

> TONGARI J <tongari95_at_[hidden]> writes:
>
> > template<typename Signature>
> > class library_function
> > {
> > public:
> > library_function(
> > shared_ptr<HMODULE> library, Signature* library_funtion)
> > :
> > m_library(library),
> > m_function(library_function)
> > {}
> >
> > operator Signature*() const { return m_function; }
> >
> >
> > private:
> > shared_ptr<HMODULE> m_library; Signature* m_function;
> > };Note: code not tested
>
> This works well. The key was `operator Signature*()`. Clever idea,
> thanks!
>
> I didn't expect it to work because I though `boost::function` would pull
> out the `Signature*` via the cooersion operator, and then the
> `library_function` would be destroyed before the call was made.
> However, this code proves that it has the desired behaviour, so it must
> be keeping a copy of the `library_function` instance inside
> `boost::function`: http://ideone.com/BZvuk9.
>
> I didn't use`make_library_function` because inferring the signature
> from the raw function isn't something I need.
>
> If you would like to add this answer to the Stack Overflow qusetion,
> I'll happily give you the brownie points :)
>

Done: http://stackoverflow.com/a/20614403/2969631

BTW, the reason why I knew this trick is because I implemented this:

    http://tinyurl.com/mptekbr

and it credits to Proto.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net