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 register your callback that looks like:
void registerCallback(CallbackFunction* pCallback);
Because I’d like to capture the state of several variables to be used in the callback, I can’t simply use a plain function. What I’d prefer to use is a lambda function, but the following doesn’t compile:
auto fCallback = [](const int& i) {
cout << i << endl;
};
registerCallback(fCallback);
instead I get the error:
error C2664: 'registerCallback' : cannot convert parameter 1 from '`anonymous-namespace'::<lambda0>' to 'CallbackFunction (__cdecl *)'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I’ve been reading up on this topic a lot, and trying a few different (probably idiotic) approaches, but I can’t seem to get this to work. Casting the function allows the code to compile, but (not surprisingly) it crashes. It may be that I’ve overlooked the solution either here on StackOverflow or elsewhere, so a link will suffice. (Though, since I’m a bit new to some of these techniques, please make sure that the correspondence is clear enough for a newbie. For instance, if this conversation contains my answer, I don’t understand. Please simplify or explain the correspondence.) FYI, I’m using Visual C++ 2010.
Please let me know if there is anything I can do to clarify my question. Thanks in advance for the help!
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.