I have a C library function with the following signature,
void register_callback(void (*callback)(int, void*), void* args);
What is the best way to get this to work with, if I have a callback of the form,
std::function<void(int)>?
I have a C library function with the following signature,
void register_callback(void (*callback)(int, void*), void* args);
What is the best way to get this to work with, if I have a callback of the form,
std::function<void(int)>?
this remains valid for as long as the variable
bob
does.A copy of
bob
is not enough, the actual instance ofbob
that we took a pointer to in theregister_callback
call has to live long enough.If this is difficult, consider a smart pointer wrapping said
std::function
and doing a pointer to the storedstd::function
.There will be modest overhead in the above, in that we dispatch over a function pointer, then over the equivalent of a vtable, then inside the
std::function
again.What is going on above is that I make a stateless lambda to convert the
void* args
into a pointer-to-std::function
, and invoke thatstd::function
with theint
. Stateless lambdas can convert to function pointers implicitly.With:
You may then do