C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Lifetime of a temporary that is bound to a reference

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Mon May 21, 2007 7:42 pm    Post subject: Lifetime of a temporary that is bound to a reference Reply with quote



Hello,

I vaguely remember that when a temporary is bound to a reference, the
lifetime of the temporary is at least as long as that of the reference. I
would like to know how this is defined in the following case:

const int& min( const int& a, const int& b )
{ return a < b ? a : b; }

int f() { return 42; }

int main()
{
int i = min( f(), int() );

return 0;
}

In the example above, two temporaries are bound to the formal parameters of
min(). One of these parameters is then bound to the return value of the
function, which is then used to assign a value to 'i'. I believe that this
code is well defined, but I would like to know why. Is there a guarantee
that the temporaries will not have gone out of scope by the time 'i' is
assigned its value? I guess that the lifetime of the temporaries depends on
the lifetime of the formal parameters of min()? But if this code is well
defined, then how come the following isn't:

#include <iostream>

class Foo
{
const int& m_value;

public:
Foo( const int& value ) : m_value( value ) {}
void f() { std::cout << m_value << std::endl; }
};

Foo x( 42 );

int main()
{
// Output is "1" on VC++
// 2005 Express Edition.
x.f();
}

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Markus Schoder
Guest





PostPosted: Tue May 22, 2007 3:35 am    Post subject: Re: Lifetime of a temporary that is bound to a reference Reply with quote



On Mon, 21 May 2007 13:42:11 -0600, Matthias Hofmann wrote:
Quote:
Hello,

I vaguely remember that when a temporary is bound to a reference, the
lifetime of the temporary is at least as long as that of the reference.
I would like to know how this is defined in the following case:

const int& min( const int& a, const int& b ) { return a < b ? a : b; }

int f() { return 42; }

int main()
{
int i = min( f(), int() );

return 0;
}

In the example above, two temporaries are bound to the formal parameters
of min(). One of these parameters is then bound to the return value of
the function, which is then used to assign a value to 'i'. I believe
that this code is well defined, but I would like to know why. Is there a
guarantee that the temporaries will not have gone out of scope by the
time 'i' is assigned its value? I guess that the lifetime of the
temporaries depends on the lifetime of the formal parameters of min()?

The lifetime of temporaries bound to parameters of a function lasts until
the full expression the function is part of is evaluated. So this example
has well defined behaviour.

Quote:
But if this code is well defined, then how come the following isn't:

#include <iostream

class Foo
{
const int& m_value;

public:
Foo( const int& value ) : m_value( value ) {} void f() { std::cout
m_value << std::endl; }
};

Foo x( 42 );

int main()
{
// Output is "1" on VC++
// 2005 Express Edition.
x.f();
}

The lifetime of temporaries bound to a constructor's parameters lasts
until the initialisation is completed. So this example has undefined
behaviour.

--
Markus Schoder


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.