Can I use libcurls CURLOPT_WRITEFUNCTION with a C++11 lambda expression?

You actually can do that by casting the lambda function to function pointer. You can first make a typedef to make cast easier.

typedef size_t(*CURL_WRITEFUNCTION_PTR)(void*, size_t, size_t, void*);

Then you use static_cast.

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, static_cast<CURL_WRITEFUNCTION_PTR>(curl_callback));

Note: In order to convert to C function pointer, you can only use empty captures [].


libcurl is plain C library, you need to set a callback that can be called from a such. This means funny C++ things need to be "C'ified" first to work. Like into an old-style function pointer.

This is also addressed in the libcurl FAQ entry "Using C++ non-static functions for callbacks?"

See also: C-style Callbacks in C++11