The compiler should only emit code that acts as-if , it runs your code, that is, the observed behavior is the same.
The likely access point should be either the assignment operator in bar , as you need to create the original vector or copy constructor.
In your example, the compiler can rewrite it differently if it can be explained that v is never used in bar anymore, so it can implement the call foo(v); like foo(std::move(v)) (e.g. @MSalters already mentioned). This leaves v as a dead object in bar() , which is then destroyed.
Alternatively, the compiler can simply create a vector in the call stack and save the call to the destructor itself, effectively making a free call by default.
source share