Are returned locals automatically xvalues

Posted by mark on Stack Overflow See other posts from Stack Overflow or by mark
Published on 2012-04-01T11:25:26Z Indexed on 2012/04/01 11:30 UTC
Read the original article Hit count: 231

Filed under:
|

Following on from a comment I made on this:

passing std::vector to constructor and move semantics Is the std::move necessary in the following code, to ensure that the returned value is a xvalue?

std::vector<string> buildVector()
{
  std::vector<string> local;

  // .... build a vector

  return std::move(local);
}

It is my understanding that this is required. I have often seen this used when returning a std::unique_ptr from a function, however GManNickG made the following comment:

It is my understanding that in a return statement all local variables are automatically xvalues (expiring values) and will be moved, but I'm unsure if that only applies to the returned object itself. So OP should go ahead and put that in there until I'm more confident it shouldn't have to be. :)

Can anyone clarify if the std::move is necessary?

Is the behaviour compiler dependent?

© Stack Overflow or respective owner

Related posts about c++

Related posts about move