Convert lambda with capture clause stored in std::function to raw function pointer

喜你入骨 提交于 2019-12-01 16:41:43

You can't, because a lambda which captures is a closure, so it has state (it is an object with instance variables). A function pointer has no state. Thus, you cannot do this without either 1) the API you are using that requires the function pointer also allows you to pass a user data argument where you pass the state, or 2) storing the state in a global variable or something.

Search around Stack Overflow for "member function to callback" and you will get an idea (basically, you are wanting to use a member function, the operator(), as a callback).

user1095108

You can convert a capturing lambda/functor into a function pointer, but you need to be careful when doing it:

https://codereview.stackexchange.com/questions/79612/c-ifying-a-capturing-lambda

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