Are
std::vector<double> foo ()
{
std::vector<double> t;
...
return t;
}
and
std::vector<double> foo ()
{
std::vector<double> t;
...
return std::move (t);
}
equivalent ?
More precisely, is return x
always equivalent to return std::move (x)
?
std::move is totally unnecessary when returning from a function, and really gets into the realm of you -- the programmer -- trying to babysit things that you should leave to the compiler.
std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object.
std::move() is a function used to convert an lvalue reference into the rvalue reference. Used to move the resources from a source object i.e. for efficient transfer of resources from one object to another. std::move() is defined in the <utility> header.
std::move is actually just a request to move and if the type of the object has not a move constructor/assign-operator defined or generated the move operation will fall back to a copy.
They're not equivalent, and you should always use return t;
.
The longer version is that if and only if a return statement is eligible for return value optimization, then the returnee binds to rvalue reference (or colloquially, "the move
is implicit").
By spelling out return std::move(t);
, however, you actually inhibit return value optimization!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With