[EMCpp]Item-27 Alternatives to Overloading on Universal References
· ☕ 5 min read
Universal reference parameters often have efficiency advantages, but they typically have usability disadvantages.
Universal reference parameters often have efficiency advantages, but they typically have usability disadvantages.
Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.
But don’t apply std::move
or std::forward
to local objects if they would otherwise be eligible for the return value optimization.
If a function template parameter has type T&&
for a deduced type T
, or if an object is declared using auto&&
, the parameter or object is a universal reference.
std::move
performs an unconditional cast to an rvalue, while std::forward
casts its argument to an rvalue only if that argument is bound to an rvalue.