 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tobias Güntner Guest
|
Posted: Wed Aug 18, 2004 6:46 pm Post subject: Question about lifetime of objects |
|
|
Hi!
I've got a question about the lifetime of the temporary (?) A object in
the following example:
class A {};
class B {
B(const A& aaa = A()) {}
};
void foo(const B& b = B())
{
}
How long does aaa (i.e. the A object, not the reference) exist? Does it
still exist when foo() gets called?
The Holy Standard says "Temporary objects are destroyed as the last step
in evaluating the full-expression (intro.execution) that (lexically)
contains the point where they were created" [12.2/3] - Does that mean
that it is safe to access the A() that has been bound to aaa within foo()?
--
Regards,
Tobias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu Aug 19, 2004 12:07 pm Post subject: Re: Question about lifetime of objects |
|
|
Tobias Güntner wrote:
| Quote: | How long does aaa (i.e. the A object, not the reference) exist? Does it
still exist when foo() gets called?
|
I believe this is an open issue.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
zvesko Guest
|
Posted: Fri Aug 20, 2004 12:52 am Post subject: Re: Question about lifetime of objects |
|
|
"Tobias Güntner" <guentner (AT) schleissheimer (DOT) de> wrote
| Quote: | Hi!
I've got a question about the lifetime of the temporary (?) A object in
the following example:
class A {};
class B {
B(const A& aaa = A()) {}
};
void foo(const B& b = B())
{
}
How long does aaa (i.e. the A object, not the reference) exist? Does it
still exist when foo() gets called?
|
Yes it does. Your code with some extra trace:
class A
{
public:
A() { cout<<"A constructedn"; }
~A(){ cout<<"A destroyedn"; }
};
class B
{
public:
B(const A & aaa=A()) { cout<<"B constructedn"; }
~B(){ cout<<"B destroyedn";}
};
void foo(const B & bbb = B()) { cout<<"foo calledn"; }
calling foo with no parameters will print:
A constructed
B constructed
foo called
B destroyed
A destroyed
because it is the same as calling foo(B(A()))
both B, bound to bbb, and A, bound to aaa, are auto object and their
lifetime is
within the block in which they were created, that is within foo. they are
destroyed
in the reverse order of creation.
| Quote: | The Holy Standard says "Temporary objects are destroyed as the last step
|
A to which aaa is bound is not a temporary object. Temporary objects
are nameless, compiler generated objects and, being nameless,
you can not refer to them explicitly.
| Quote: | in evaluating the full-expression (intro.execution) that (lexically)
contains the point where they were created" [12.2/3] - Does that mean
|
that means that in a expression like
string s1, s2, s3;
string s = s1 + s2 + s3/*1*/;
the compiler will generate a temporary object to hold the result of s2 + s3.
that object will be deleted at point 1 which is the end of the expression
that caused it's creation.
| Quote: | that it is safe to access the A() that has been bound to aaa within foo()?
|
In the above example aaa is not accessible from foo.
VZ
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Fri Aug 20, 2004 1:33 am Post subject: Re: Question about lifetime of objects |
|
|
Tobias Güntner wrote in news:cg05er$shh$07$1 (AT) news (DOT) t-online.com in
comp.lang.c++.moderated:
| Quote: | Hi!
I've got a question about the lifetime of the temporary (?) A object
in the following example:
class A {};
class B {
B(const A& aaa = A()) {}
};
void foo(const B& b = B())
{
}
How long does aaa (i.e. the A object, not the reference) exist? Does
it still exist when foo() gets called?
The Holy Standard says "Temporary objects are destroyed as the last
step in evaluating the full-expression (intro.execution) that
(lexically) contains the point where they were created" [12.2/3] -
Does that mean that it is safe to access the A() that has been bound
to aaa within foo()?
|
No 12.2/4 : "... contexts in which temporaries are destroyed ...
appears as an initializer for a declarator defining an object. In
that context, the temporary that holds the result of the expression
shall persist until the object’s initialization is complete. ... "
foo above can be rewriten:
void foo( B const &b = B( A() ) );
and a call to foo() can be rewriten:
foo( B( A() ) );
Clearly in the above, the 'A()' temporary "appears as an initializer"
in the declarator of the 'B' passed to foo. Hence the 'A' temporary
will be destroyed as soon as B::B( A const & ) completes.
This ("consuming of initializers") allows:
struct C
{
A a;
C( A const &aa = A() ) : a( aa ) {}
};
A f() { return A(); /* for example */ }
C c = f();
c.a can be directly intialized by f()'s return value,
no copying required.
So its not all bad news .
Rob.
--
http://www.victim-prime.dsl.pipex.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
johnchx Guest
|
Posted: Fri Aug 20, 2004 10:13 am Post subject: Re: Question about lifetime of objects |
|
|
Tobias Güntner <guentner (AT) schleissheimer (DOT) de> wrote
| Quote: | Does that mean
that it is safe to access the A() that has been bound to aaa within foo()?
|
How do you propose to access that object within foo()?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tobias Güntner Guest
|
Posted: Tue Sep 28, 2004 10:40 am Post subject: Re: Question about lifetime of objects |
|
|
Rob Williscroft wrote:
| Quote: | No 12.2/4 : "... contexts in which temporaries are destroyed ...
appears as an initializer for a declarator defining an object. In
that context, the temporary that holds the result of the expression
shall persist until the object’s initialization is complete. ... "
foo above can be rewriten:
void foo( B const &b = B( A() ) );
and a call to foo() can be rewriten:
foo( B( A() ) );
Clearly in the above, the 'A()' temporary "appears as an initializer"
in the declarator of the 'B' passed to foo. Hence the 'A' temporary
will be destroyed as soon as B::B( A const & ) completes.
|
That seems logical to me. However, as always , VC6 does something
different ;)
(The reason why I wanted such "persistent temporaries" was that I needed
some storage to convert various data types before calling foo() -
Fortunately I found a much better & more portable solution using a
traits class which provides all necessary information )
Thanks,
Tobias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rani Sharoni Guest
|
Posted: Wed Sep 29, 2004 5:48 pm Post subject: Re: Question about lifetime of objects |
|
|
Tobias Güntner <fatbull (AT) users (DOT) sourceforge.net> wrote
| Quote: | Rob Williscroft wrote:
No 12.2/4 : "... contexts in which temporaries are destroyed ...
appears as an initializer for a declarator defining an object. In
that context, the temporary that holds the result of the expression
shall persist until the objectâ??s initialization is complete. ... "
[...]
(The reason why I wanted such "persistent temporaries" was that I needed
some storage to convert various data types before calling foo() -
Fortunately I found a much better & more portable solution using a
traits class which provides all necessary information )
|
The point of destruction of a call argument temporary by J. Stephen Adamczyk:
http://www.codesourcery.com/cxx-abi/argument-destruction.pdf
Enjoy,
Rani
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tobias Güntner Guest
|
Posted: Fri Oct 01, 2004 11:53 am Post subject: Re: Question about lifetime of objects |
|
|
Rani Sharoni wrote:
That means if a parameter is passed by value, its copy is destructed
immediately after the function returns, and if a conversion is necessary
(no matter whether a parameter is passed by value or by reference), the
necessary temporary object will be destroyed after the full expression
has been evaluated. If a conversion is necessary, the parameter copy may
be elided. Right?
I altered the example program a little:
#include <iostream>
using namespace std;
struct A {
A() { cout << "A::A()n"; }
A(const A&) { cout << "A::A(A&)n"; }
~A() { cout << "A::~A()n"; }
} a;
struct B {
operator A() { cout << "B: p A()n"; return A(); }
} b;
int f(A)
{
cout << "f calledn";
return 0;
}
int g(const A&)
{
cout << "g calledn";
return 0;
}
void h(int i)
{
cout << "h calledn";
}
int main()
{
cout << "---n";
// Object destroyed on return from function
cout << "n1)n";
h(f(a));
// Object destroyed at end of full expression
// (if no elision done, parameter destroyed on
// return, temporary destroyed at end of full
// expression)
cout << "n2)n";
h(f(b));
// no copy/temporary
cout << "n3)n";
h(g(a));
// Object destroyed at end of full expression
cout << "n4)n";
h(g(b));
cout << "---n";
return 0;
}
The output is (VC6/VC Toolkit 2003):
A::A()
---
1)
A::A(A&)
f called
A::~A()
h called
2)
B: p A()
A::A()
f called
A::~A()
h called
3)
g called
h called
4)
B: p A()
A::A()
g called
h called
A::~A()
---
A::~A()
Output (2) confuses me. According to the PDF above, B needs to be
converted into A and that temporary will be copied for the parameter.
This copy has been elided, so only the temporary A is left. But IMO this
A should be destroyed at the end of full expression (i.e. after h
returns), not after f returns. Am I missing something?
--
Regards,
Tobias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|