Should templated functions take lambda arguments by value or by rvalue reference?

FunctorT&& is a universal reference and can match anything, not only rvalues. It’s the preferred way to pass things in C++11 templates, unless you absolutely need copies, since it allows you to employ perfect forwarding. Access the value through std::forward<FunctorT>(f), which will make f an rvalue again if it was before, or else will leave it as an lvalue. Read more here about the forwarding problem and std::forward and read here for a step-by-step guide on how std::forward really works. This is also an interesting read.

FunctorT& is just a simple lvalue reference, and you can’t bind temporaries (the result of a lambda expression) to that.

Leave a Comment