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

后端 未结 2 1125
旧巷少年郎
旧巷少年郎 2021-01-18 06:29

Since my last recent question was unfortunately worded and resulted in a solution to another problem then mine, here I will try to formulate my actual problem in a clear way

相关标签:
2条回答
  • 2021-01-18 06:48

    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).

    0 讨论(0)
  • 2021-01-18 07:01

    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

    0 讨论(0)
提交回复
热议问题