Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 behaviour: std::cout << g(a)->n << std::endl; /*undefined behaviour*/.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 behaviour.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading