A point from N3290 C++ draft, § 12.2, 5th point, line 10.
The second context is when a reference is bound to a temporary. 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 in a new-initializer (5.3.4) persists until the completion of the full-expression containing the new-initializer. [ Example:
struct S { int mi; const std::pair<int,int>& mp; }; S a { 1, {2,3} }; S* p = new S{ 1, {2,3} };// Creates dangling reference
— end example ] [ Note: This may introduce a dangling reference, and implementations are en开发者_如何学Ccouraged to issue a warning in such a case. — end note ]
This is the added point when compared to C++03. But the example is not understandable for me. Can you please explain this point with any other example?
I know what dangling references and temporary objects are and that std::pair
holds two values of possibly different data types.
Temporaies in general last only to the end of the expression that they were created in:
#include <complex>
void func()
{
std::complex<int> a; // Real variable last until the end of scope.
a = std::complex<int>(1,2) + std::complex<int>(3,4);
// ^^^^^^^^^^^^^^^^^^^^^^ Creates a temporary object
// This is destroyed at the end of the expression.
// Also note the result of the addition creates a new temporary object
// Then uses the assignment operator to change the variable 'a'
// Both the above temporaries and the temporary returned by '+'
// are destroyed at ';'
If you create a temporary object and bind it to a reference. You extend its lifespan to the same lifespan of the reference it is bound too.
std::complex<int> const& b = std::complex<int>(5,6);
// ^^^^^^^^^^^^^^^^ Temporary object
// ^^^^ Bound to a reference.
// Will live as long as b lives
// (until end of scope)
The exception to this rule is when the temporary is bound to a reference in a new initializer.
S* p1 = new S{ 1, {2,3} };
// This is the new C++11 syntax that does the `equivalent off`:
S* p2 = new S {1, std::pair<int,int>(2,3) };
// ^^^^^^^^^^^^^^^^^^^^^^^ Temporary object.
// This lives until the end of the
// expression that belongs to the new.
// ie the temporary will be destroyed
// when we get to the ';'
But here we are binding the new temporary object to the member
const std::pair<int,int>& mp;
This is a const reference. But the temporary object it is bound to will be destroyed at the ';' in the above expression so mp will be a reference to an object that no longer exists when you try and use it in subsequent expressions.
}
精彩评论