Skip to main content

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.



Comments

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.

Scott
Sumant 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 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 cpp-programs.blogspot.com and my email is sareneeru94@gamil.com

Popular Content

Unit Testing C++ Templates and Mock Injection Using Traits

Unit testing your template code comes up from time to time. (You test your templates, right?) Some templates are easy to test. No others. Sometimes it's not clear how to about injecting mock code into the template code that's under test. I've seen several reasons why code injection becomes challenging. Here I've outlined some examples below with roughly increasing code injection difficulty. Template accepts a type argument and an object of the same type by reference in constructor Template accepts a type argument. Makes a copy of the constructor argument or simply does not take one Template accepts a type argument and instantiates multiple interrelated templates without virtual functions Lets start with the easy ones. Template accepts a type argument and an object of the same type by reference in constructor This one appears straight-forward because the unit test simply instantiates the template under test with a mock type. Some assertion might be tested in

Covariance and Contravariance in C++ Standard Library

Covariance and Contravariance are concepts that come up often as you go deeper into generic programming. While designing a language that supports parametric polymorphism (e.g., templates in C++, generics in Java, C#), the language designer has a choice between Invariance, Covariance, and Contravariance when dealing with generic types. C++'s choice is "invariance". Let's look at an example. struct Vehicle {}; struct Car : Vehicle {}; std::vector<Vehicle *> vehicles; std::vector<Car *> cars; vehicles = cars; // Does not compile The above program does not compile because C++ templates are invariant. Of course, each time a C++ template is instantiated, the compiler creates a brand new type that uniquely represents that instantiation. Any other type to the same template creates another unique type that has nothing to do with the earlier one. Any two unrelated user-defined types in C++ can't be assigned to each-other by default. You have to provide a

Multi-dimensional arrays in C++11

What new can be said about multi-dimensional arrays in C++? As it turns out, quite a bit! With the advent of C++11, we get new standard library class std::array. We also get new language features, such as template aliases and variadic templates. So I'll talk about interesting ways in which they come together. It all started with a simple question of how to define a multi-dimensional std::array. It is a great example of deceptively simple things. Are the following the two arrays identical except that one is native and the other one is std::array? int native[3][4]; std::array<std::array<int, 3>, 4> arr; No! They are not. In fact, arr is more like an int[4][3]. Note the difference in the array subscripts. The native array is an array of 3 elements where every element is itself an array of 4 integers. 3 rows and 4 columns. If you want a std::array with the same layout, what you really need is: std::array<std::array<int, 4>, 3> arr; That's quite annoying for