5 ms·
file_stream<unsignedchar>::open_istream(L"myfile.txt").then([](basic_istream<unsignedchar> fileStream) I don't think I've ever seen this C++ idiom before (eith
by CamperBob2 14y ago
file_stream<unsignedchar>::open_istream(L"myfile.txt").then([](basic_istream<unsignedchar> fileStream)
I don't think I've ever seen this C++ idiom before (either the ".then" method or the use of anonymous brackets like that.) Is this common everywhere or just in Windows circles?
- ksherlock 14y ago.then() is just a method. The "anonymous brackets" is a c++11 lambda function. I don't think chaining it like that is common anywhere, yet.
- apaprocki 14y agoThe .then() method appears to be something on concurrency::task, which is part of the Microsoft's Parallel Pattern Library. This article says: "we've started a new project called Casablanca to build a modern C++ library for cloud-based client-server communication, in which all concurrent operations are exposed as PPL tasks." http://www.drdobbs.com/parallel/improving-futures-and-callbacks-in-c-to/240004255?pgno=2 http://www.drdobbs.com/parallel/improving-futures-and-callba... Link to Microsoft's C++ committee proposal (N3327): http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3327.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n332...
- HNLogInShit 14y agoUgh. It's "client/server", not "client-server".
- twoodfin 14y agoThat looks like Microsoft's new "async everywhere" approach to API design. It's probably a pretty good idea in concept, but I hope they don't expect C++ developers to actually write those awful nested lambda .then() blocks for what are conceptually sequential actions.
- marshray 14y agoI expect you can define named functions for your "conceptually sequential actions" if you prefer.
- twoodfin 14y agoSure, but is that much better? It reminds me of having to create named struct classes to simulate closures prior to C++11 lambdas. The language syntax just doesn't support that style of programming.
- marshray 14y agoSo how many languages actually do support this style directly? The C# async keyword looks pretty clean, just give C++ a decade or so to catch up.
- MichaelGG 14y agoIs there a better approach available in C++? In other languages you'd use monads or other special compiler forms that can rewrite sequential looking code into the horrible multi-nested callback/error handling code it needs to be.
- ajasmin 14y agoBoost Coroutines perhaps? http://www.boost.org/doc/libs/1_53_0/libs/coroutine/doc/html/index.html http://www.boost.org/doc/libs/1_53_0/libs/coroutine/doc/html...
- Figs 14y agoI've used that style of programming in some of my hobby projects (mixed with other obvious sequencing operations like wait_until(predicate) and delay(time)) for doing timeline scripting. In my experience, it tended to become a mess when I needed things more complex than linear action sequences (e.g. conditionals, loops, etc. are better suited to a real interpreter). As far as common uses of chaining for sequencing in C++, the only common use that comes to mind is iostreams operator <<, but that predates lambdas.
- pjmlp 14y agoThis is C++11 code. You should look at it, it makes C++ feel a new language.
- HNLogInShit 14y agoI've never seen this before either: obj[L"key1"] = json::value::boolean(false); What is that L doing inside the brackets?
- alpatters 14y agoI think the use of then() makes sense as this is an async call and not serial. So the function passed to then() is a completion handler, i.e. it is run when the call is complete. So do this call, and then when its finsihed do this. I like it.