
2013/12/16 Alexander Lamaison
TONGARI J
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.