The following is an anti-pattern:
auto f() {
std::vector<int> v(100000);
return std::move(v); // no need to use std::move thanks to RVO (return value optimization)
}
Using a std::move can even produce worst code (see here)
However, what should I do in the following situation:
auto f() {
std::vector<int> v0(100000);
std::vector<int> v1(100000);
return std::make_pair(std::move(v0),std::move(v1)); // is the move needed?
}
For the second snippet,
returnreturns the result of thestd::make_pair()function. That's an RValue.However, the OP's question probably condenses to whether (or why not) Named Return Value Optimization still applies to
v0/v1when returned as astd::pair.Thereby, it's overlooked that
v0/v1aren't subject ofreturnanymore, but become arguments ofstd::make_pair(). As such,v0/v1are LValues –std::move(v0), std::move(v1)have to be applied to turn them into RValues if move-semantic is intended.Demo on coliru:
Output: