Re: a question on execution order
Mycroft Holmes wrote:
Hi,
a language question: what does f() return? is it safe and predictable?
does the standard say anything about the execution order of
destructors of local objects with respect to "producing the return
value"?
this is what I'd deduce, but I may be wrong: 't' is constructed after
'result', so it should be destroyed before: t's destructor will run
when 'x' is still a valid reference, so I'd bet the code won't crash.
since the result may be a local object, I'd expect local destructors
to run AFTER the result is in a safe place, so 'f' will return 6789,
whatever the compiler, the optimization level...
struct tricky
{
int& x;
~tricky() { x = 1234; }
};
int f()
{
int result = 6789;
tricky t = { result };
return result;
}
even if my above conjecture is correct, what if the compiler applies
NVRO and turns 'f' to something like:
void f(int& result)
{
result = 6789;
tricky t = { result };
}
I don't think NRVO has anything to do with the behaviour of 'f'.
Looking at the original 'f', the return value is formed by the
expression in the 'return' statement. By the time the expression
is evaluated, 't' is still very much alive, and 'result' has the
value 6789.
Since the return is by value, a temporary of type 'int' is copy-
constructed [from 'result'], so the return statement will cause
the function to return 6789.
Now, what happens at the closing curly brace is immaterial here,
really. 't' upon destruction will change the local object's
value, which will have no influence on the return value of 'f'.
If you did
int f(int& outer)
{
tricky t = { outer };
return outer;
}
int main() {
int a = 6789;
int b = f(a);
return 0;
}
Then, still, in 'main' at the 'return' statement, 'b' would have
the old value of 'a' and 'a' would have the new value (1234).
Of course, if you did
int& f(int& outer)
{
tricky t = { outer };
return outer;
}
*then* the return value would change along with the argument.
I think I didn't leave anything out...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask