Looking at this question makes me wondering how it can be implemented. Here is the code borrowed from the question:
std::vector<int> return_vector(void)
{
std::vector<int> tmp {1,2,3,4,5};
return tmp;
}
std::vector<int> &&rval_ref = return_vector();
So tmp
is allocated from the stack of function return_vector
, now this stack memory is re-used by rval_ref
which sits at upper level stack. There are 2 separate stacks here, is there a memory copy going under the hood? If so, it consumes CPU cycles which seems against the semantics of "move".
But if not, the lower level stack table must be segmented, i.e. a portion is carved out so that when the stack is released, that portion can be saved.
I do not know compiler implementation, just a curiosity which is hard to find answer. Or maybe I am totally missing something.
[UPDATE] Thanks for Ben's prompt reply, yes, vector actually allocated heap memory. It is a bad example, but I see comment mentioning that it is actually one memory region managed by caller stack, this makes sense to me. Looking forward to more in-depth reply.