Suppose that I have the following (trimmed down) code:
class P { P(); P(const P&); ~P(); }
void foo(P x) {
...
}
void bar() {
P p{};
foo(p); // compiler uses P::(const P&) to construct the value for x
...
// compiler calls P::~P() on p
}
The compiler must create a copy of p in order to call foo, so the caller invokes the copy constructor before the call. My question is, who is in charge of destroying this created object? There seem to be two valid choices:
- The callee (i.e.
foo) calls the destructor on all of its by-value arguments before it returns and then the caller deallocates the memory (by popping it off the stack). - The callee doesn't do anything, and the caller (i.e.
bar) calls the destructor on all of the temporaries before the sequence point at the end of thefoo(p)call.