Can I use a lambda function or std::function object in place of a function pointer?

前端 未结 4 941
-上瘾入骨i
-上瘾入骨i 2021-02-13 21:04

I\'ve got a library that I need to use that defines the following:

typedef void CallbackFunction(const int& i);

and has a function to regis

4条回答
  •  日久生厌
    2021-02-13 21:09

    You can't use lambdas, because your registerCallback function expects pointer to non-member function. The only way to pass additional parameter to free standing function is 1) using global (static) data or 2) making it a template taking compile time constants, e.g.

    template void callback(const int& i)
    { /* can use data */ }
    
    registerCallback(callback<10>);
    

提交回复
热议问题