Wednesday, October 10, 2012

C++11 Idioms @ 2012 Silicon Valley Code Camp

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.


Michal Mocny said...

Hi there, just a few 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 Meyers said...

I think it would have been helpful to see a discussion of the use of perfect forwarding in your discussion of constructor parameters. I've written a blog entry on this topic, which tries to build on the foundation you laid. I'd be interested in your reaction to it.


Sumant Tambe said...

@Michal: Sigh! It turns out operator+ is not the best way to motivate the need for the in idiom. The real point is that when you don't know if you will make a copy or not at the interface of the function. Imagine big chunks of code at the beginning and at the end of of a function. Only in the middle, some optimization related to rvalues may take place. In such cases, you don't want to replicate the big blocks of code in all 2^N functions you would write when you use rvalue references (not universal).

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.

Sumant Tambe said...

@Scott: The idea was to provide a gradual ramp to the audience because I feel perfect forwarding is an advanced feature meant for generic library developers. The "Book" example I used is meant to suggest application-level code. I've posted a much longer answer on your blog post explaining additional shortcomings of perfect forwarding in application code.

Cpp Programs said...

Hey bro my name is Neeraj and i have blog which is related to c++ programming. I want to exchange link between us. If you are interested than let me know. My blog url is and my email is