问题
I have this code (simplified version):
const int& function( const int& param )
{
return param;
}
const int& reference = function( 10 );
//use reference
I can't quite decide to which extent C++03 Standard $12.2/5 wording
The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference...
is applicable here.
Is reference
variable in the code above valid or dangling? Will the reference in the calling code prolong the lifetime of the temporary passed as the parameter?
回答1:
A full-expression is an expression that is not a subexpression of another expression. In this case, the full-expression containing the call function( 10 )
is the assignment expression:
const int& reference = function( 10 );
In order to call function
with the argument 10
, a temporary const-reference object is created to the temporary integer object 10
. The lifetime of the temporary integer and the temporary const-reference extend through the assignment, so although the assignment expression is valid, attempting to use the integer referenced by reference
is Undefined Behavior as reference
no longer references a live object.
The C++11 Standard, I think, clarifies the situation:
The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:
...
— A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.
"The temporary to which the reference is bound ... persists for the lifetime of the reference". In this case, the lifetime of the reference ends at the end of the assignment expression, as does the lifetime of the temporary integer.
回答2:
This will compile, but you will end up with a dangling reference. param
is freed after function
returns.
- function is called with a reference to the temporary, anonymous object
- function returns the reference
- now that function has returned the temporary param is released
- the reference is now dangling as the object was destroyed
If you had made it non-const, then it wouldn't have compiled because you cannot pass a non-const reference to an anonymous object.
回答3:
From the C++11 viepoint a reference returned by a function is not a temporary:
12.12.1 Temporaries of class type are created in various contexts: binding a reference to a prvalue (8.5.3), returning a prvalue (6.6.3), a conversion that creates a prvalue (4.1, 5.2.9, 5.2.11, 5.4), throwing an exception (15.1), entering a handler (15.3), and in some initializations (8.5).
A function returning a reference dosn't return prvalue ("pure rvalue"), so it's not a temporary. This seems quite natural: compiler can't manage lifetime of referenced objects, it is programmer's responsibility
Thus, the compiler doesn't provide any liftime guarantes for const int& reference since it is not bounded to temporary.
回答4:
It is this part that is important
The temporary to which the reference is bound
In this case the parameter is bound to the temporary, and will be destroyed after the call.
You cannot extend the lifetime further by passing the reference on.
来源:https://stackoverflow.com/questions/10190677/will-a-reference-bound-to-a-function-parameter-prolong-the-lifetime-of-that-temp