Let's suppose I have following code:
int bar = 0;
struct Foo {
~Foo() { bar = 1; }
};
int main(int argc, char ** argv) {
Foo f;
return bar;
}
What will be return value of program? 0 or 1?
From [stmt.return]/3:
The copy-initialization of the returned entity is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables (6.6) of the block enclosing the return statement.
So the destructor runs after the return value has been initialized, and the return value is thus 0 on the first call of your function.
Automatic variables are removed in the reverse order to their declaration.
So the return value of the function is established before the call to ~Foo().
The return of foobar is therefore a very well-defined 0.
Your question would be more interesting if your function returned int&.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With