I have read several answers and and many articles about move semantic so it is simply a static cast to rvalue reference, this question is about its influence on the stack, so is the stack left fragmanted after move? or is it rearranged somehow? as the stack moved objects are not subject to stack unwinding. The following snippet runs fine:
#include <memory>
#include <cassert>
struct A {
int x = 0;
};
struct B {
A a;
};
void SetB(B& b) {
A a1{6}; //pushing into the stack.
A a2{7}; //pushing into the stack.
b.a = std::move(a2);
}
int main()
{
B b;
SetB(b);
assert( b.a.x == 7);
}
a2 is defined in SetB stack frame but still available after SetB is finished and the contorl back to main. However; a1 is not despite it is closer to main stack frame?
There's no 'stack' in the C++ standard, it's an implementation detail. So a 'generic' answer is difficult as an implementation can have any kind of effect on the given architecture's stack.
That said, the
std::move()does not remove the argument from the stack, it remains in a valid-but-undefined state. For your own classes, you might even implement it. Therefore, it's not possible to 'remove' the variable from anywhere - unless, of course, in the optimization phase of the compiler, if it's not accessed anymore.a2is not available afterSetBis finished. The value is moved tob.a, therefore,b.ais expected to contain the value thata2has contained before, but it's not the same variable (nor the same memory address).