The slides for my C++11 Idioms talk @ 2012 Silicon Valley Code Camp are now available. If you attended the talk please evaluate it. The slides for many other talks in the C++/C++11 track are also available.
Last year I reviewed the pre-print manuscript of Manning's Functional Programming in C++ written by Ivan Čukić. I really enjoyed reading the book. I enthusiastically support that the book Offers precise, easy-to-understand, and engaging explanations of functional concepts. Who is this book for This book expects a reasonable working knowledge of C++, its modern syntax, and semantics from the readers. Therefore, reading this book might require a companion book for C++ beginners. I think that’s fair because FP is an advanced topic. C++ is getting more and more powerful day by day. While there are many FP topics that could be discussed in such a book, I like the practicality of the topics selected in this book. Here's the table of contents at a glance. This is a solid coverage of functional programming concepts to get a determined programmer going from zero-to-sixty in a matter of weeks. Others have shared their thoughts on this book as well. See Rangarajan Krishnamo...
Comments
- "Perfect forwarding references" -- those which bind to l/rvalue -- are starting to be called "universal references". Scott Meyers has just written a blog post pointing to an article of his in Overload magazine about this topic. Lets help make the name catch on!
- I'm not sold on your In idiom. I agree that templated & constrained perfect forwarding functions are wordy (hopefully static_if will fix that some day), but warping the implementation of the function to include conditional branching is almost certainly worse -- both in computational runtime and programming complexity. Also, did you benchmark the pass-by-value implementations w/ optimizations in bleeding edge versions of compilers? I imagine most should do a good job optimizing away the copies and be quite fast (see the David Abrahams' article on 'want speed? pass by value')
- Im not sure why your matrix op+ moves into temporary r in the cases when parameter x is already an rvalue? Perhaps this was just to have a single look and feel leading up to last example?
- Your third idiom is very interesting, but seems a bit rough around the edges. I'de be interested to play around with this tactic a bit. Also, I wish we had language syntax for creating/manipulating tuples (like initializer lists do, e.g. python tuples are so much easier to use). Currently tuples are unwieldily beasts, though its obviously a hard problem.
- I had no idea about forward_as_tuple! cool, thanks!
- You did not really describe piecewise_construct_t or why it arguably breaks encapsulation (though I can speculate).
You've given me a lot to research, so thanks a bunch, but honestly I'de have to say that I doubt any of these idioms will enter my daily routine. Hopefully they will be useful to library developers ;)
Scott
You point out correctly that there is no need to move temporary object in r. You could just use the rvalue reference (which by the way becomes a lvalue when you call it by name).
std::piecewise_construct_t does not break encapsulation nor does std::pair. The third idiom, however, exposes implementation details outside class only when user code is written to exploit that knowledge. Perfect forwarding tuple constructor does not by itself break encapsulation. Therefore, only stable libraries should use it.