How can I initialize a boost::function
object with a raw function pointer?
Metacode
extern "C"
{
class Library
{
...
};
Library* createLibrary();
}
...
void* functionPtr = library.GetFunction("createLibrary");
boost::function<Library*()> functionObj(functionPtr);
Library* libInstance = functionObj();
If you need additional information's just let me know.
void*
is not a function pointer, so you can't create a boost::function
from it. You probably want to convert this to a proper function pointer first. How to do that is implementation dependent.
This is how this ugly conversion is recommended in POSIX (rationale):
void* ptr = /* get it from somewhere */;
Library* (*realFunctionPointer)(); // declare a function pointer variable
*(void **) (&realFunctionPointer) = ptr // hack a void* into that variable
Your platform may require different shenanigans.
Once you have such a pointer you can simply do:
boost::function<Library*()> functionObj(realFunctionPtr);
Library* libInstance = functionObj();