Lifetime

Below is the code showing that the lifetime of an object created in the create() function increases to the lifetime of the const ref created in main , is this correct in all cases? I mean, can we extend the life time of a temporary in certain cases by creating a link to it? Or is the compiler wrong in this particular case?

It is compiled with MSVC2005

 #include <iostream> class testClass { public: testClass() { std::cout << "in testClass " << ((void*)this) << std::endl; } ~testClass() { std::cout << "in ~testClass " << ((void*)this) << std::endl; } }; testClass create() { return testClass(); } int main() { { testClass const& obj = create(); std::cout << "we got a const reference to obj " << ((void*)&obj) << std::endl; } return 0; } 

Output

 in testClass 0018FF13 we got a const reference to obj 0018FF13 in ~testClass 0018FF13 

Of course, others can get different addresses ... In the above case, I was expecting a destructor for an object created using the create() function, will be called up to the line

 std::cout << "we got a const reference to obj " << ((void*)&obj) << std::endl; 
Performed

.

+4
source share
3 answers

This is a special case: binding a const reference to a temporary object stretches its lifetime until this const reference goes beyond. This is true only for references to local function constants, for example. The following will not work:

 struct X { int const& i X(int const& i_) : i(i_) {} }; int f(); int main() { X x(f()); int u = xi; //! } 

During construction x , i_ will be bound to the temporary returned by f , like i , but although this is a const reference, this temporary lifetime will not be extended to the value i , i.e. the rule applies here.

See this GOTW article

Update: as stated in the article and in the comments, const is vital. The C ++ standard allows you to bind temporary resources only to references to the constant lvalue and rvalue, therefore int& i = f(); not allowed. However, MSVC has an extension that allows this, and, as is the case with other links, the lifetime of a temporary extension is extended until the link goes out of scope. I would not recommend using this extension, as it makes the code intolerable. In fact, I would be careful when linking temporary links to links, since this function is not known, and your colleagues may be confused to see that it works, which means that the code will not have readability.

+6
source

To clarify, we can show 3 scripts for testClass create() :

1

Return a copy, but its search by constant link

 testClass create() { return testClass(); } testClass const &obj = create(); 

It extends the lifetime of a temporary testClass() , while obj .

2

Returning a copy and intercepting it by assignment (RVO)

 testClass create() { return testClass(); } testClass obj = create(); 

It extends the life of the temporary testClass() as long as obj , since RVO is implicitly applied to it. Better to say, in fact, there is no temporary object, everything works on obj even in create() .

3

Return a copy and bind it using assignment (without RVO)

 testClass create() { return testClass(); } testClass obj = create(); 

The lifetime of the temporary testClass() longer after returning from create() , and a new object comes into the world.

+2
source

This link should help you understand how this situation is qualified.

When a temporary object is created to initialize the reference variable, the name of the temporary object has the same scope as the control variable. When a temporary object is created during the evaluation of a complete expression (an expression that is not a subexpression of another expression), it is destroyed as the last step in its evaluation, which lexically contains the point at which it was created.

There are two exceptions to destroying full expressions:

  • The expression appears as an initializer for the declaration defining the object: the temporary object is destroyed when the initialization is complete.
  • The link is attached to a temporary object: the temporary object is destroyed at the end of the reference lifetime.
+1
source

All Articles