Using boost :: bind () via C code, will it work?

Is it possible to use boost::bind(mycallback, this, _1, _2) through C code?

Update

The short answer is no , boost bind does not return a pointer to a function that can be called in C code, but a functor (C ++ object with an overloaded operator () ) see below.

+4
source share
3 answers

The best way to do what you want to do is to create a C callback, which then calls the boost :: function, which is stored in some user memory with new ones.

Example:

 void callFunction(void* data) { boost::function<void(void)> *func = (boost::function<void(void)>* ) (data); (*func)(); delete(func); } 

Then you simply pass this callback and set the user data (however, this is specified in libev) to be a copy of your function assigned to a new one.

Here is how you specify user data with libev: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH

+4
source

No. boost::bind returns a functor not a function pointer. The returned object is a C ++ object that has an overloaded operator() that allows it to behave like a pointer to a function in C ++ code. But this is not a pointer to a function that can be passed to C code.

+4
source

Suppose you want to use what boost::bind returns as a callback function for the C library?

If this is the case, then no, it will not work. It will not even be created, because boost::bind does not return a function pointer.

+2
source

Source: https://habr.com/ru/post/1383252/


All Articles