c++ - How to get a function pointer from lambda expressions or so? -


this question has answer here:

i have c language function called:

f_api(void(*callback)(int)) 

and have static class method callback:

struct {     static void callback(int i) {         a::count = i;     }     static count = 0; }; 

i can call function this:

f_api(&a::callback) 

however, have change callback non-static now, because have create multiple a objects.

but cannot change definition of f_api().

i tried using lambda:

f_api([this](int i)->void{this->count = i;})` 

but failed, because cannot convert lambda capture simple function pointer.

std::bind() cannot work, because of f_api() definition.

what can this? how can function pointer lambda expression? there method sidestep?

if cannot change c api don't think can situation. because c functions require information require passed in function parameters.

you can convert lambdas function pointers following

void (*ptr) () = []() {} 

lambdas without capture can implicitly converted function pointers. lambdas capture values cannot converted function pointers since have state (in form of member variables).

the usual way solve problem in c void* parameters. take @ pthread_create function. thread start function accepts function void* parameter points "context". context in situation can instance calling method on.

but if cannot change c api, afraid cannot regarding function pointers. thing can make variables global , use call methods in function pointer.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -