5 comments

[ 3.0 ms ] story [ 23.0 ms ] thread
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).

Most definitely. There's a less nice-looking macro, acall, that lets you specify capture:

    acall [&,copyMe] { … }
In most cases, though, async will be used inside an await block; everything surrounding the await stays in scope.
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.
Nice!

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.