Subject: Re: [boost] rvalue ref best practices?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-06-10 10:28:13
On 09/06/2012 22:21, Daniel Larimer wrote:
> In c++11 I would pass anything that I plan on 'storing' or 'consuming' by&&. This results in the most efficient code, but 'breaks' lvalue compatibility and forces me to consider having two (or more) versions of every method. Unfortunately, you get a explosion of permutations of rvalue and const& as the number of arguments increases and it makes getting function pointers much more 'ugly'.
> Instead of providing two (or more) overloads, one for lvalues and one for rvalue, I have decided to only provide the rvalue overload and if the user wants to pass an lvalue they wrap with std::ref(), std::cref(), std::move() or my own helper copy() which makes the copy explicit and converts the lvalue to a rvalue. (std::ref() and std::cref() could only work if the arguments are template parameters... in which case the method would probably use std::forward<>() to handle the auto-detection of move vs copy semantics.
This is a terrible design.
First, if you have many functions that plan on 'storing' or 'consuming'
their arguments, you're clearly doing something wrong.
Second, you should use templates which enable perfect forwarding. This
way you don't have a combinatorial explosion of the number of overloads,
just the one.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk