...

/

Assigning C Function Pointers to Lambdas

Assigning C Function Pointers to Lambdas

Learn about the assignment of C function pointers to lambdas as well as different lambdas and their relations.

Lambdas as function pointers

Lambdas without captures can be implicitly converted to function pointers. Let's say we are using a C library, or an older C++ library, that uses a callback function as a parameter, like this:

Press + to interact
extern void download_webpage(const char* url,void (*callback)(int, const char*));

The callback is called with a return code and some downloaded content. It is possible to pass a lambda as a parameter when calling download_webpage(). Since the callback is a regular function pointer, the lambda must not have any captures and we ...