I’m having trouble passing a C++0x lambda function as the second argument to makecontext (from ucontext.h). The signature of makecontext is:
void makecontext(ucontext_t*, void (*)(), int, ...);
Previously, I was able to apply a C-style (void (*)(void)) cast, to the global scope functions I used. A reinterpret_cast would do the trick in C++. However, with a C++0x lambda function, I get the following error:
error: invalid cast from type ‘main(int, char**)::<lambda(int)>’ to type ‘void (*)()’
I’m using G++ 4.6. The following code is sufficient to produce the compile error:
#include <ucontext.h>
void f1(int i) {}
int main(int argc, char *argv[]) {
ucontext_t c;
makecontext(&c, (void (*)(void))f1, 1, 123); // ok
makecontext(&c, reinterpret_cast<void (*)(void)>(f1), 1, 123); // ok
auto f2 = [](int i){};
makecontext(&c, (void (*)(void))f2, 1, 123); // error
makecontext(&c, reinterpret_cast<void (*) (void)>(f2), 1, 123); // error
return 0;
}
[](int i){}is a captureless lambda that has a singleintparameter and returns nothing; it is thus implicitly convertible tovoid(*)(int): a pointer to a function that takes a singleintand returns nothing.Assuming the
makecontextfunction is capable of handling functions with different types (it appears from its documentation that it is, though the documentation is a bit vague), then you’ll need to use two casts to use it with a lambda: one to cast the lambda to a function pointer, and one to cast the lambda to thevoid(*)()type:This is all rather unsafe (casting between function pointer types isn’t exactly the safest thing to be doing), so it would be best to wrap this sort of functionality into a utility library. You can wrap the
makecontextfunction with a function template so that you can be sure that all of your uses of the function are type safe, and all of the unsafe code is encapsulated in one place.