You're talking about undefined behaviour - this is a badly broken feature of C++
because it does not move the local variable it references onto the heap or some other alternate data structure.
Look up the upwards funarg problem.
> The upwards funarg problem arises when the calling function refers to the called/exited function's state after that function has returned. Therefore, the stack frame containing the called function's state variables must not be deallocated when the function returns, violating the stack-based function call paradigm.
C++ violates the constraint we're talking about.