some time back I submitted a request to enable GCD to accept C++ lambda functions. I got notice from apple that it was implemented in Xcode 4.4. I finally decided to try it out. I don't see how to make it work. it doesn't compile. I don't see any new dispatch functions that would accept them. I'm not finding any useful info on the web.
Could I get a hint as to how to set up a dispatch call using a C++ lambda with clang and libc++?
Make sure you're compiling in Objective-C++ mode.
-Eli
just pass it as is to dispatch_async()?
Yes. Short example:
#include <dispatch/dispatch.h>
#include <cstdio>
dispatch_once_t once;
int main() {
dispatch_once(&once, []{printf("asdf\n");});
}
-Eli
thanks.
I had two problems. one was not using .mm and the other was I was attempting to use a lambda that took an argument. Since the argument was of the wrong type, the type didn't match for dispatch_async.
Question. does the lambda need to be defined in an objc++ file or can a .cpp translation unit pass the lambda to the objc++ translation unit which can then call dispatch_async()?
-James
The conversion from a lambda to a block pointer requires objc++.
-Eli
is that conversion a compile time operation or a runtime operation?
If you convert a lambda immediately to block pointer type (like my
original example), it's essentially free, as if you'd written the
equivalent block. With an expression of lambda type in general,
though, the conversion happens at runtime: the lambda is copied into
an autoreleased block.
-Eli
Thanks. It's Important because I'll need to put this in a cross platform wrapper.