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

前端 未结 4 949
-上瘾入骨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:11

    In general, no you can't use a lambda or function object where a function pointer is required. Function objects are full-fledged objects that have overloaded the application operator (()) so you can use them as functions syntactically.

    The standard algorithms are written as templates, which allows you to pass either a function address (pointer) or a function object to them, because they use the same syntax. When you pass a function object, a template is instantiated which accepts that function object; when you pass a function pointer, a different template is instantiated.

    So to get lambdas/function objects to work with your code, you would need to modify the library you're using.

提交回复
热议问题