On a return value, if the return expression refers directly to the name of a local lvalue (i.e. at this point an xvalue) there is no need for the std::move
. On the other hand, if the return expression is not the identifier, it will not be moved automatically, so for example, you would need the explicit std::move
in this case:
T foo(bool which) {
T a = ..., b = ...;
return std::move(which? a : b);
// alternatively: return which? std::move(a), std::move(b);
}
When returning a named local variable or a temporary expression directly, you should avoid the explicit std::move
. The compiler must (and will in the future) move automatically in those cases, and adding std::move
might affect other optimizations.