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

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:37:59

问题


Can I use boost::bind(mycallback, this, _1, _2) across C code?

Update

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


回答1:


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

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 it is specified in libev) to be a copy of your function allocated with new.

This 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




回答2:


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




回答3:


I assume you want to use whatever boost::bind returns as a callback function for a C library?

If that's the case, then no, it won't work. It won't even build, as boost::bind does not return a function pointer.



来源:https://stackoverflow.com/questions/8282599/using-boostbind-across-c-code-will-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!