Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why isn't the lifetime of the temporary extended in this case?

Reply
Thread Tools

Why isn't the lifetime of the temporary extended in this case?

 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-21-2008
Let's assume we have a class like this:

//---------------------------------------------------------
#include <iostream>

class MyClass
{
public:
MyClass() { std::cout << "constructor\n"; }
~MyClass() { std::cout << "destructor\n"; }

const MyClass& print(int i) const
{
std::cout << i << std::endl;
return *this;
}
};
//---------------------------------------------------------

Now, if I create a reference to a temporary instance of this class,
the lifetime of that instance will be extended for the lifetime of the
reference. For example:

//---------------------------------------------------------
int main()
{
std::cout << "Before\n";
const MyClass& obj = MyClass(); //*
std::cout << "After\n";
obj.print(2);
}
//---------------------------------------------------------

This program will print:

Before
constructor
After
2
destructor

This is even so if the temporary is the return value of a function.
For example, let's assume we have the function:

MyClass getMyClass() { return MyClass(); }

Now if we change the line marked with //* to this:

const MyClass& obj = getMyClass(); //*

the result will still be the same. So clearly the lifetime of the return
value of a function is extended by the reference.

Now comes the puzzling part, and my actual question. Suppose that we
change the line marked with //* to this:

const MyClass& obj = MyClass().print(1); //*

Suddenly the output changes:

Before
constructor
1
destructor
After
2

Now the temporary object is destroyed after the reference assignment
ends! The second print() call is now calling a destroyed object! (Oddly
gcc doesn't issue even a warning about this.)

The same is true for:

const MyClass& obj = getMyClass().print(1); //*

But why? Why does the print() function returning a reference to itself
change the semantics of the lifetime of the temporary object? Why isn't
the reference extending the lifetime of the object anymore? Why does the
reference extend the lifetime of the return value of getMyClass(), but
not the lifetime of the return value of MyClass:rint()? How does it
make even sense that a reference can be created to an object which is
destroyed immediately after the reference is created?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-22-2008
Victor Bazarov wrote:
>> How does it
>> make even sense that a reference can be created to an object which is
>> destroyed immediately after the reference is created?

>
> The same way that a pointer can be created to an object that has already
> been destroyed:


But I thought the whole idea of references is that they would be safer
than pointers.
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      08-22-2008
>> class MyClass
>> {
>> public:
>> MyClass() { std::cout << "constructor\n"; }
>> ~MyClass() { std::cout << "destructor\n"; }
>>
>> const MyClass& print(int i) const
>> {
>> std::cout << i << std::endl;
>> return *this;
>> }
>> };
>> //---------------------------------------------------------


>> //---------------------------------------------------------
>> int main()
>> {
>> std::cout << "Before\n";
>> const MyClass& obj = MyClass(); //*
>> std::cout << "After\n";
>> obj.print(2);
>> }
>> //---------------------------------------------------------
>>


>>
>> MyClass getMyClass() { return MyClass(); }
>>
>> Now if we change the line marked with //* to this:
>>
>> const MyClass& obj = getMyClass(); //*
>>
>> the result will still be the same. So clearly the lifetime of the return
>> value of a function is extended by the reference.
>>
>> Now comes the puzzling part, and my actual question. Suppose that we
>> change the line marked with //* to this:
>>
>> const MyClass& obj = MyClass().print(1); //*
>>
>> Suddenly the output changes:
>>
>> Before
>> constructor
>> 1
>> destructor
>> After
>> 2
>>

[...]
> Imagine:
>
> // using your 'MyClass' class
> MyClass const& pass(MyClass const& arg) { return arg; }
>
> MyClass const& bad = pass(pass(pass(pass(MyClass()))));
>
> Some might think that it is the same reference initialised by binding it
> to the temporary being passed in and out of the 'pass' function and
> eventually put into the 'bad' reference. But it *isn't*! The argument
> of the 'pass' function and its return value are *different references*.
> The return value is initialised from the argument initialised from the
> temporary. So, if the rule was only about binding a ref to a temporary,
> the temporary would only lives as long as the argument of the very first
> 'pass' function (the inner-most). The "until the full expression is
> evaluated" requirement would override that in this case, so you should
> see the temporary report its destruction right after the last 'pass'
> returns, just before 'bad' is initialised.
>
> > How does it
>> make even sense that a reference can be created to an object which is
>> destroyed immediately after the reference is created?

>
> The same way that a pointer can be created to an object that has already
> been destroyed:
>
> Object* foo(Object *p) {
> delete p;
> return p;
> }
>
> Any use of the return value of this 'foo' function will have undefined
> behaviour.


I didn't quite understand this. Do you say that the call:
obj.Print(2);
is an undefined behavior?

Would this:
MyClass const& bad = pass(pass(pass(pass(MyClass()).print(3))));
be UB as well?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"lifetime of temporary bound to reference..." Igor R. C++ 2 01-28-2009 09:40 AM
Temporary Object Lifetime better_cs_now@yahoo.com C++ 5 06-13-2008 12:20 PM
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
lifetime of temporary object from function return & optimization pt C++ 8 06-01-2005 01:11 AM



Advertisments