Wednesday, 11 September 2013

Pass a c++ lambda to old c function pointer

Pass a c++ lambda to old c function pointer

I have to create a c++ wrapper to a old c library.
In a class method I must call a c function that takes with other stuff
also a function pointer(it is a event handler and the function takes a
function that is fired when a event happens).
A simple example of it is this:
void myclass::add_handler(std::function<void()> handler, otherstuff...)
{
/*
* Many things.
*/
type_of_function_pointer_accepted_by_old_c_library_add_handler
nameofvariable =
[handler](same_arguments_as_in_definition_of_the_old_c_function_pointer_accepted)
{
/*
* Many other things with other stuff to be done before the
* handler but always only when fired and not when
myclass::add_handler is called.
*/
handler();
};
old_c_library_add_handler(nameofvariable);
/*
* Last things.
*/
}
The compiler complains, as I know, that I can't assign a lambda with
capture to an old c function pointer. The question is: how can I do to
solve?

No comments:

Post a Comment