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 

RTTI lifetimes

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





PostPosted: Fri Feb 27, 2004 6:16 pm    Post subject: RTTI lifetimes Reply with quote



Is this safe?

template<class T>
class AClass
{
std::type_info const& getType() const { return typeid(T); }
};

The question was raised internally about returning a reference to (what
appears to be) a temporary. Yet intuition says that the RTTI stuff is not
actually constructed in response to a typeid statement.

Is there a lifetime issue here?
--
<disclaimer>
Opinions posted are those of the author.
My company doesn't pay me enough to speak for them.
</disclaimer>
--
Jim Melton
Software Architect, Fusion Programs
Lockheed Martin IS&S
(303) 971-3846




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Mar 01, 2004 12:30 pm    Post subject: Re: RTTI lifetimes Reply with quote



Jim Melton <jim.melton (AT) lmco (DOT) com> wrote


Quote:
Is this safe?

template class AClass
{
std::type_info const& getType() const { return typeid(T); }
};

The question was raised internally about returning a reference to
(what appears to be) a temporary. Yet intuition says that the RTTI
stuff is not actually constructed in response to a typeid statement.

Typically it isn't, but the standard does allow lazy construction
(something like a singleton, for example). However, §5.2.8/1: "The
result of a typeid expression is an lvalue [...]. The lifetime of the
object referred to by the lvalue extends to the end of the programm."

Quote:
Is there a lifetime issue here?

There shouldn't be, except maybe in some cases with dynamically linked
objects. (The standard doesn't say anything about dynamic linking, but
I wouldn't be surprised that if you unload the object containing the
class, the type_info describing it disappear as well.)

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

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