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.
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
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.