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 

Compiler generated temporaries

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





PostPosted: Wed Sep 28, 2005 2:27 am    Post subject: Compiler generated temporaries Reply with quote



Given this:

class A { std::string x; public: std::string get_name() { return x; }
};

class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };

.....
A a;
....
B b(a.get_name());

is the value of b:Mad well defined - or is it taking a reference to a
temporary which gets deleted? (And if so, why doesn't the compiler tell
me!?)

Thanks


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

Back to top
kanze
Guest





PostPosted: Wed Sep 28, 2005 11:05 am    Post subject: Re: Compiler generated temporaries Reply with quote



ThosRTanner wrote:
Quote:
Given this:

class A { std::string x; public: std::string get_name() { return x; }
};

class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };

....
A a;
...
B b(a.get_name());

is the value of b:Mad well defined - or is it taking a
reference to a temporary which gets deleted?

It's a dangling reference; using it is undefined behavior.

Quote:
(And if so, why doesn't the compiler tell me!?)

Because you've spread the problem out over several different
functions, and the compiler, which normally only looks at one
function at a time, can't see it.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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


Back to top
Emil Kirichev
Guest





PostPosted: Wed Sep 28, 2005 11:07 am    Post subject: Re: Compiler generated temporaries Reply with quote



The reference will be invalid when the destructor of A is called. So,
as far as 'a' exists, 'b' is in a good state. Kinda dangerous.


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

Back to top
ravinderthakur@gmail.com
Guest





PostPosted: Wed Sep 28, 2005 11:08 am    Post subject: Re: Compiler generated temporaries Reply with quote

this is not a legal way of doing it!!!

you will be storing an object that would have been destroyed.
the compiler cannot detect such errors because the compiler
does only syntax checking and not the semantic checking.

in ur code the syntax is write and only the semantic is wrong.

thanks
rt


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

Back to top
Maciej Sobczak
Guest





PostPosted: Wed Sep 28, 2005 11:08 am    Post subject: Re: Compiler generated temporaries Reply with quote

ThosRTanner wrote:
Quote:
Given this:

class A { std::string x; public: std::string get_name() { return x; }
};

class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };

....
A a;
...
B b(a.get_name());

is the value of b:Mad well defined

When? Till the end of this expression it is well defined and refers to
the temporary that is returned by get_name.
After than, b.x becomes a dangling reference, because there is nothing
that would force that temporary to live longer that this single expression.

Quote:
is it taking a reference to a
temporary which gets deleted?

Right.

Quote:
(And if so, why doesn't the compiler tell
me!?)

Good question. The standard does not prevent the compiler from telling
you, so the compiler *might* warn you about this. On the other hand, the
standard also does not prevent you from writing this kind of code, and
in general it is very difficult (or even impossible) to analyse all such
cases. Here it looks straightforward, because everything is in the same
file, but imagine that B's ctor is compiled separately...


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

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


Back to top
Yuri Khan
Guest





PostPosted: Wed Sep 28, 2005 11:09 am    Post subject: Re: Compiler generated temporaries Reply with quote

ThosRTanner wrote:
Quote:
Given this:

class A { std::string x; public: std::string get_name() { return x; }
};

class B { std::string const &x; public: B(std::string const &a) : x(a)
{} };

....
A a;
...
B b(a.get_name());

is the value of b:Mad well defined - or is it taking a reference to a
temporary which gets deleted? (And if so, why doesn't the compiler tell
me!?)

A little experimentation shows that the reference is valid until the
end of B's constructor. After that, the temporary gets deleted, and the
reference is left dangling.

C++ Standard, 2003, 12.2/5:
The temporary to which the reference is bound or the temporary that is
the complete object to a subobject of which the temporary is bound
persists for the lifetime of the reference except as specified below. A
temporary bound to a reference member in a constructor's
ctor-initializer (12.6.2) persists until the constructor exits.


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


Back to top
ThosRTanner
Guest





PostPosted: Wed Sep 28, 2005 1:51 pm    Post subject: Re: Compiler generated temporaries Reply with quote

Thanks for all the helpful responses. BTW, you'll be glad to hear this
wasn't my code, I was trying to work out what was going wrong (but only
going wrong sometimes!).


[ 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.