why are template functors not passed as r-value references in functions from STL?
First of all, they will redirect links, not links to rvalue.
However, like many βwhyβ questions about the design of the language as a whole, the answer may be simple: because no one has proposed this change (see how to send an offer ). I suspect that a lot of function objects passed to standard library algorithms, either lambdas or stateless, or are extremely cheaply copied. Moreover, if you have such an expensive object, you can always turn it into an inexpensive copy for the purpose of the algorithm:
call_me(std::ref(huge_object));
Finally, some of these algorithms rely on passing objects of these functions to other helpers. If the algorithm simply assumes that the function object is βfreeβ to copy, this simplifies coding. If we introduce the possibility of rvalues ββhere, this will add another level of questions that need to be addressed - are we just going to the link? What about function objects with ref-qual operator() ?
Some combination of the above probably explains why, for example, this is still:
template <class InputIt, class UnaryPredicate> InputIt find_if(InputIt, InputIt, UnaryPredicate );
but not
template <class InputIt, class UnaryPredicate> InputIt find_if(InputIt, InputIt, UnaryPredicate&& );
Barry
source share