Board index » cppbuilder » Relying on RTTI type names.

Relying on RTTI type names.


2008-02-10 03:34:56 AM
cppbuilder83
With the Borland C++ compiler (currently using 5.92, BCB2007), can I count
on typeid(SomeType).name() being exactly the same for a given type on all
systems I compile on? Can I count on it staying the same in future versions
of the compiler (in the short term, not like, 10 years from now)?
Right now I'm using the type names to identify subclasses of some base class
(using dynamic_cast isn't convenient here); and it's working well and
letting me stay away from more complex setups. I know there are better
solutions, but if the type names are reliable I don't want to reinvent the
wheel.
Thanks,
Jason
 
 

Re:Relying on RTTI type names.

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
With the Borland C++ compiler (currently using 5.92, BCB2007), can I count
on typeid(SomeType).name() being exactly the same for a given type on all
systems I compile on? Can I count on it staying the same in future
versions
if I'm not mistaken, there is no requirement for uniqueness
of the result of std::type_info::name() for different classes
Quote
of the compiler (in the short term, not like, 10 years from now)?
no, the compiler vendor could change implementation-defined behaviour
Quote
Right now I'm using the type names to identify subclasses of some base
class
why not boost::is_base_of or std::tr1::is_base_of
Quote
(using dynamic_cast isn't convenient here); and it's working well and
letting me stay away from more complex setups. I know there are better
solutions, but if the type names are reliable I don't want to reinvent the
wheel.
Cheers,
Serge
 

Re:Relying on RTTI type names.

Thanks for your reply.
"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
Quote
if I'm not mistaken, there is no requirement for uniqueness
of the result of std::type_info::name() for different classes

>of the compiler (in the short term, not like, 10 years from now)?

no, the compiler vendor could change implementation-defined behaviour
Yes. I was mostly wondering about Borland, specifically. I don't mind
relying on compiler-specific behavior for this particular application. I
suppose a more accurate question is: Does the 5.92 compiler generate unique
names, and does Borland have any plans to switch them around for version
5.93?
Quote
>Right now I'm using the type names to identify subclasses of some base
>class

why not boost::is_base_of or std::tr1::is_base_of
Well, checking dynamic_cast results would get the job done fine for what I'm
doing, except I don't have the class types available at the point in the
code where I'm checking for certain types; so I have to rely on some kind of
ID string. It's sort of this weird, incomplete class factory setup -- it's
beyond my control. I'm using type_info::name() as a string ID for classes
like this:
BaseClass *obj = something(...);
AnsiString objtype = typeid(*obj).name();
I was just wondering because it was convenient -- it only took a second to
use and I didn't need to bother modifying any other code outside this one
function that I'm working on that is using the type info, but it's not too
hard for me to just add a simple ID string to each class that I define
myself, which is a better way to do it in the end:
BaseClass *obj = something(...);
AnsiString objtype = obj->MyOwnTypeStringFromSomewhere();
Thanks,
Jason
 

{smallsort}

Re:Relying on RTTI type names.

Jason Cipriani < XXXX@XXXXX.COM >wrote:
Quote
Thanks for your reply.

"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
news:47ae2249$ XXXX@XXXXX.COM ...
>if I'm not mistaken, there is no requirement for uniqueness
>of the result of std::type_info::name() for different classes
Actually, there's not requirement for that result
at all. A standard-conforming compiler could return
empty strings, or only return empty string for some
types, or only at some times, or...
Quote
>>of the compiler (in the short term, not like, 10 years from now)?
>
>no, the compiler vendor could change implementation-defined behaviour

Yes. I was mostly wondering about Borland, specifically. I don't mind
relying on compiler-specific behavior for this particular application. I
suppose a more accurate question is: Does the 5.92 compiler generate unique
names, and does Borland have any plans to switch them around for version
5.93?
They probably do, but then, 5.93 might just as well be
an internal-only release and -- probably for very well-
founded performance reasons -- 5.94, which might become
the next official release, might just as well break this.
Or not. Or...
Quote
>>Right now I'm using the type names to identify subclasses of some base
>>class
>
>why not boost::is_base_of or std::tr1::is_base_of

Well, checking dynamic_cast results would get the job done fine for what I'm
doing, except I don't have the class types available at the point in the
code where I'm checking for certain types; so I have to rely on some kind of
ID string. It's sort of this weird, incomplete class factory setup -- it's
beyond my control. I'm using type_info::name() as a string ID for classes
like this:

BaseClass *obj = something(...);
AnsiString objtype = typeid(*obj).name();

I was just wondering because it was convenient -- it only took a second to
use and I didn't need to bother modifying any other code outside this one
function that I'm working on that is using the type info, but it's not too
hard for me to just add a simple ID string to each class that I define
myself, which is a better way to do it in the end:

BaseClass *obj = something(...);
AnsiString objtype = obj->MyOwnTypeStringFromSomewhere();
How about using 'std::type_id'? It's sortable (it has
'operator<'), guaranteed to be unique, and will stay
the same for the duration of a process. Or do you really
need a string (as in "a textual description")? Then a
'std::map<std::type_id,std::string>' might be worth
setting up.
Quote
Thanks,
Jason
Schobi
--
XXXX@XXXXX.COM is never read
I'm HSchober at gmx dot de
"The trouble with being a god is that you've got
no one to pray to." Terry Pratchett
 

Re:Relying on RTTI type names.

I appreciate your reply.
"Hendrik Schober" < XXXX@XXXXX.COM >wrote in message
Quote
How about using 'std::type_id'? It's sortable (it has
'operator<'), guaranteed to be unique, and will stay
the same for the duration of a process. Or do you really
need a string (as in "a textual description")? Then a
'std::map<std::type_id,std::string>' might be worth
setting up.
Well... so what I'm actually doing is (and please remain seated), I have a
dialog with a TNotebook (well actually it's a TPageControl but for the
purpose of this explanation it's a TNotebook) and I need to display some
different components depending on whatever subclass I'm dealing with. It's a
long and twisted story how it got this way, so don't worry about that. I set
the TNotebook page names to the class names of the subclasses, and I'm using
typeid(*obj).name() to pick the active page. I just needed to be quick and
dirty about it.
But in the end I'm just going to add some GetClassName() base method that
returns the class name (which I'm typing in by hand in each subclass
implementation), and use that instead. Adding methods to these classes is
not incredibly convenient in my situation -- but it's not that hard either
and I won't have to worry about typeid changing.
I know that VCL stuff in particular relies on the RTTI a lot (for example,
the object names in your designer's DFM files -- when you write a new VCL
component you don't define an object name separately, it just uses the class
name from the RTTI to do it), so it seemed like I would be able to safely do
the same -- and it's just so convenient. I don't know. I don't want to put
any more time into thinking about this application right now.
Jason
 

Re:Relying on RTTI type names.

Jason Cipriani wrote:
Quote
I know that VCL stuff in particular relies on the RTTI a lot (for example,
the object names in your designer's DFM files -- when you write a new VCL
component you don't define an object name separately, it just uses the
class name from the RTTI to do it), so it seemed like I would be able to
safely do the same -- and it's just so convenient. I don't know. I don't
want to put any more time into thinking about this application right now.
If your classes derive from TObject, you could use ->ClassName().
Jon
 

Re:Relying on RTTI type names.

Oh... nice! That's exactly the kind of thing I was looking for. :)
Thanks!
Jason
"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
Jason Cipriani wrote:
>I know that VCL stuff in particular relies on the RTTI a lot (for
>example, the object names in your designer's DFM files -- when you write
>a new VCL component you don't define an object name separately, it just
>uses the class name from the RTTI to do it), so it seemed like I would be
>able to safely do the same -- and it's just so convenient. I don't know.
>I don't want to put any more time into thinking about this application
>right now.

If your classes derive from TObject, you could use ->ClassName().

Jon

 

Re:Relying on RTTI type names.

"Jason Cipriani" < XXXX@XXXXX.COM >writes:
Quote
Thanks for your reply.

"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
news:47ae2249$ XXXX@XXXXX.COM ...
>if I'm not mistaken, there is no requirement for uniqueness
>of the result of std::type_info::name() for different classes
>
>>of the compiler (in the short term, not like, 10 years from now)?
>
>no, the compiler vendor could change implementation-defined behaviour

Yes. I was mostly wondering about Borland, specifically. I don't mind
relying on compiler-specific behavior for this particular application. I
suppose a more accurate question is: Does the 5.92 compiler generate unique
names, and does Borland have any plans to switch them around for version
5.93?
Even if there were no plans to chance, I don't believe that it's wise
to assume that the plans are firm. For one thing, something may come
along unforseen that forces their hand.
However, I think it's already true that the C++Builder compiler does
not generate unique names. Admittedly, I haven't used BCB since
version 6, so maybe that has changed, but I recall that templates did
NOT reflect the parameter types.
I don't know about other types, or where the name length limitations
may kick in. I'm seeming to recall only 255 characters of unique
characters in the exported names in .obj files, which may possibly
also be truncated in the type_info structure's name the same
way... but of that IM" not entirely sure.)
--
Chris (TeamB);
 

Re:Relying on RTTI type names.

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
Right now I'm using the type names to identify subclasses of some
base class (using dynamic_cast isn't convenient here)
Why?
Gambit
 

Re:Relying on RTTI type names.

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
Well, checking dynamic_cast results would get the job done fine for
what I'm doing, except I don't have the class types available at the point
in the code where I'm checking for certain types; so I have to rely on
some kind of ID string. It's sort of this weird, incomplete class factory
setup -- it's beyond my control. I'm using type_info::name() as a string
ID for classes like this:
Why do you need to identify the classes at all? Polymorphism is designed to
hide those details from you, so why not use it the way it is meant to be
used? What are you trying to accomplish exactly?
Gambit
 

Re:Relying on RTTI type names.

"Jason Cipriani" < XXXX@XXXXX.COM >wrote in message
Quote
But in the end I'm just going to add some GetClassName() base
method that returns the class name (which I'm typing in by hand
in each subclass implementation), and use that instead.
VCL classes already have a ClassName() method inherited from TObject.
Quote
I know that VCL stuff in particular relies on the RTTI a lot
On Delphi RTTI, yes. Not on C++ RTTI.
Quote
(for example, the object names in your designer's DFM files -- when
you write a new VCL component you don't define an object name
separately, it just uses the class name from the RTTI to do it)
It uses TObject::ClassName() for that.
Gambit
 

Re:Relying on RTTI type names.

Jason Cipriani < XXXX@XXXXX.COM >wrote:
Quote
[...] so it seemed like I would be able to safely do
the same -- and it's just so convenient. I don't know. I don't want to put
any more time into thinking about this application right now.
Here's what I'd do: I'd hide all this behind a nice
interface which might just as well be implemented using
a 'std::map<std::type_id,TNotebook*>' abd add a big and
ugly comment saying that this relies on type id names
being unique.
So, when there's a problem with this and you need to
change the implementation to something else, you can do
so without having to change much of the code.
Quote
Jason
Schobi
--
XXXX@XXXXX.COM is never read
I'm HSchober at gmx dot de
"The trouble with being a god is that you've got
no one to pray to." Terry Pratchett
 

Re:Relying on RTTI type names.

Hendrik Schober" < XXXX@XXXXX.COM >wrote in message
Quote
Jason Cipriani < XXXX@XXXXX.COM >wrote:
>[...] so it seemed like I would be able to safely do
>the same -- and it's just so convenient. I don't know. I don't want to
>put
>any more time into thinking about this application right now.

Here's what I'd do: I'd hide all this behind a nice
interface which might just as well be implemented using
a 'std::map<std::type_id,TNotebook*>' abd add a big and
ugly comment saying that this relies on type id names
being unique.
That's basically what I ended up doing; although I just stuck a ": public
TObject" after the base class name (heh), and then made the TNotebook page
names correspond to the class names:
MyNotebook->ActivePage = MyObject->ClassName();
Very little code changed, works great for now, and a big ugly comment made
me feel a lot more secure about it :)
Thanks,
Jason
Quote
So, when there's a problem with this and you need to
change the implementation to something else, you can do
so without having to change much of the code.