Skip to content

Instantly share code, notes, and snippets.

@moleike moleike/heap-lambda.cc

Last active Aug 29, 2015
Embed
What would you like to do?
Lambda in a C callback
#include <functional>
#include <iostream>
// using a lambda in a C callback
extern "C" void
register_callback (void (*callback)(void *), void * context)
{
// for simplicity we just call it
callback (context);
}
extern "C" void
invoke_function (void* ptr)
{
(*static_cast<std::function<void()>*>(ptr))();
}
class example
{
public:
example (int val) : a(val)
{
register_callback (invoke_function,
new std::function<void()>([=](){
std::cout << "value is " << a;
})
);
}
private:
int a;
};
int main()
{
example ex(42);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.