Why is the destructor of a function parameter not called at the end of the function, but at the end of the full expression making the function call?

Viewed 86

Consider the following example:

#include <iostream>

struct A
{
    int n = 0;
    A() { std::cout << "A()" << std::endl; }
    A(const A&) { std::cout << "A(const A&)" << std::endl; }
    ~A() { std::cout << "~A()" << std::endl; }
};

int f(A a) { std::cout << "f()" << std::endl; return 1; }

int main()
{
    A a;
    std::cout << f(a) << " was returned from f(A)" << std::endl;
    std::cout << "leaving main..." << std::endl;
}

I expect the output of this program to be:

A()
A(const A&)
f()
~A()
1 was returned from f(A)
leaving main...
~A()

because when the function returns, its parameters should have already been destroyed. For example, if we have the following function:

A* g(A a) { std::cout << "g()" << std::endl; return &a; }

Dereferencing the returned pointer is undefined behavior:

std::cout << g(a)->n << std::endl; // undefined behavior

But the output is:

A()
A(const A&)
f()
1 was returned from f(A)
~A()
leaving main...
~A()

I'm looking for an explanation of this behavior.

1 Answers

Why is destructor for a function parameter not called at the end of the function but at the end of the full expression containing the function call?

According to [expr.call]/7

It is implementation-defined whether the lifetime of a parameter ends when the function in which it is defined returns or at the end of the enclosing full-expression. The initialization and destruction of each parameter occurs within the context of the calling function.

So the standard allows either behaviour.

I'm looking for an explanation of this behaviour.

It's permitted by the standard, and that's how whichever compiler you tested chose to organize its code.

Related