Sumit said:
Don't you think that would contradict 12.2/2. I'm quoting the relevant
line only (the whole thing sounds terribly confusing anyway -- like
everything else in the Standard):
A temporary bound to the returned value in a function return statement *************************
(6.6.3) persists until the function exits.
That's a different case.
We are not talking about what happens during the execution of the
return statement. The case under discussion happens after the function
has exited.
The above applies in situations like this
std::string foo()
{
return std::string();
}
Here a temporary string object is created for the return value. That temporary
exists until the function exits (has terminated). But that is very different
to what you actually do with the returned object:
const std::string& a = foo();
In foo()'s return statment a temporary was constructed. The return type of
foo() specifies that foo returns 'by value' thus a copy of that temporary
is to be made. And since that second temporary is bound to a reference, it
will live on as long as the reference exists. But the temporary created during
execution of the return statement is not the same temporary that gets bound
to the reference.