 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jim Melton Guest
|
Posted: Fri Feb 27, 2004 6:16 pm Post subject: RTTI lifetimes |
|
|
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
|
Posted: Mon Mar 01, 2004 12:30 pm Post subject: Re: RTTI lifetimes |
|
|
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 |
|
 |
|
|
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
|
|