[xsd-users] how to get type information from tree nodes

Boris Kolpackov boris at codesynthesis.com
Wed Aug 21 14:16:22 EDT 2013


Hi Lena,

Lena Herscheid <lenaherscheid at gmail.com> writes:

> Is it possible with cxx-tree to generate member functions returning the
> typename of the current node (as string)? 

No, there is no such support.


> I'm asking because the alternative way of efficiently determining the type
> of any during runtime requires some RTTI which is not portable.

I am not sure what you mean by "some RTTI", but standard C++ RTTI (i.e.,
dynamic_cast, typeid) are very much portable. Some projects may choose
to disable this support in order to minimize code size, but that's
another story.

So if you need a type name in C++/Tree, then typeid(x).name () is
the recommended way. If, however, you want to find whether x is of 
a particular dynamic type, then use dynamic_cast, it will be much
faster than any custom RTTI with string comparison:

base& x = ...

if (derived1* d1 = dynamic_cast<derived1*> (&x))
{
  // x is derived1
}
else if (derived2* d2 = dynamic_cast<derived2*> (&x))
{
  // x is derived2
}

...

else
{
  // x is something else
}

Boris



More information about the xsd-users mailing list