Re: Temporary Object Lifetime
On Jun 13, 12:34 pm, Ucayaly Fish <UcayalyF...@gmail.com> wrote:
That's irrelevant. The fact that a reference is bound to a
temporary has no effect on the lifetime of the temporary. The
only time the lifetime of the temporary is extended is when the
temporary is used to initialize the reference,
Is it a compiler deficiency or regular standard complience if
additional copy constructor is called during such an initialization?
Yes:-).
Seriously, it depends on what version of the standard you're
looking at. All of the currently approved versions *require* a
copy (and then authorize it to be optimized away).
Slightly modified sample with string member and a pointer:
===code tmptst1.cpp ===
#include <iostream>
#include <cstdlib>
#include <string>
class Foo {
public:
Foo( const std::string& str ) : msg(str), p(0)
{std::cerr << "up foo" << std::endl;
p = new char[10];}
Foo( const Foo& src ) : msg( "bad foo" ), p(0)
{std::cerr << "copy foo" << std::endl;
p = new char[20];}
~Foo()
{std::cerr << "die foo" << std::endl;
delete[] p;}
void doSomeThing() const { std::cout << msg << std::endl; }
private:
std::string msg;
char* p;
};
class Bar {
public:
Bar( const std::string& str ): m_Foo( str )
{std::cerr << "up bar" << std::endl;}
~Bar(){std::cerr << "die bar" << std::endl;}
const Foo &GetFoo() const { return m_Foo; }
Foo m_Foo;
};
int main() {
// const Foo &OneFoo = Bar( "Hi there!" ).GetFoo();
const Foo &OneFoo = Bar( "Hi there!" ).m_Foo;
std::cerr << "do stuff with OneFoo?" << std::endl;
OneFoo.doSomeThing();
}
The standard isn't really very clear here. My interpretation
was that it more or less required the Bar object to be
destructed at the end of the full expression, while requiring
that the Foo object have its lifetime extended (and that this
was the motivation behind "allowing" the copy). This section
has been reworked some in the current draft, and FROM MEMORY, I
think the draft forbids the copy, and requires extension of the
Bar object's (the containing object's) lifetime. We're in the
middle of moving here, however, and my copies of the various
versions of the standard are on a network which is not reachable
for the moment, so I can't verify this one way or the other.
===code tmptst1.cpp ===
produces this output if compiled with MSVC.NET 2008
cl /EHsc /O2 /NDEBUG tmptst1.cpp
up foo
up bar
copy foo
die bar
die foo
do stuff with OneFoo?
bad foo
die foo
It's definitely conform with C++03, and from memory, it's also
what g++ does (but since NIS isn't working, I can only access
local disks on my Unix machines, so I can't try anything).
Arguably, the compiler is required to call Bar's destructor at
the end of the full expression, and if it does, the only way to
extend the lifetime of the temporary is to make a copy.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34