I had a quick look in the source to see how this works (there is obviously some macro fun going on). The macro cleverness (which I hadn't seen before) was:
#define async __r << [&]()
By writing 'async { ... }', we can turn any scope into a lambda function.
Obviously this will require some care (in particular make sure the function is evaluated before we leave any scope with any used local variables).
This has me thinking about doing Node.JS-style event processing with the new C++ lambda syntax. I guess you can do it, but you must always copy [=] into the closure so that the io function can return:
void fred()
{
int first = 'x';
int second = 'y';
wait_key([=](int c) {
if (c == x) {
wait_key([=](int d) {
if (d == y) {
printf("got x and y\n");
}
});
}
});
}
wait_key(void (*func)(int c)) registers a callback then returns. When a key is processed func is called.
I use Ghostery which blocks the 'Github Ribbon' by default - it took me a while to find the repo ( https://github.com/Sidnicious/team ) since it wasn't linked prominently elsewhere on the page.
5 comments
[ 3.0 ms ] story [ 23.0 ms ] threadObviously this will require some care (in particular make sure the function is evaluated before we leave any scope with any used local variables).
I use Ghostery which blocks the 'Github Ribbon' by default - it took me a while to find the repo ( https://github.com/Sidnicious/team ) since it wasn't linked prominently elsewhere on the page.
Thanks for sharing.