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