4 ms·
Show HN: Team – Coroutines and async semantics for C++
- CJefferson 12y agoI 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).
- Sidnicious 12y agoMost 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.
- jhallenworld 12y agoThis 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.
- mkup 12y agoThis approach to porting node.js library to C++11 is implemented in node.native library: https://github.com/d5/node.native https://github.com/d5/node.native
- j_s 12y agoNice! I use Ghostery which blocks the 'Github Ribbon' by default - it took me a while to find the repo ( https://github.com/Sidnicious/team https://github.com/Sidnicious/team ) since it wasn't linked prominently elsewhere on the page. Thanks for sharing.