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

后端 未结 2 1159
旧巷少年郎
旧巷少年郎 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).

提交回复
热议问题