[xsd-users] Inheritance : Ways to get the real type

Boris Kolpackov boris at codesynthesis.com
Tue Nov 29 09:54:42 EST 2011


Hi Joël,

Klaim - Joël Lamotte <mjklaim at gmail.com> writes:

> Well if a there was a value at runtime providing the name or some
> identifier of the real type, I would have used that instead of
> dynamic_cast, for performance reasons, as the information would have been
> generated at code generation.
> As it's not a big concern for my crurent project I'm fine with using
> type_id.name() as it is known to be faster than dynamic_cast.

Why not use (typeid(x) == typeid(y))? Most modern compilers (e.g, GCC)
will translate this to something like this:

x->__vptr->type_info == y->__vptr->type_info

Where type_info is the address of the std::type_info object. In other
words, it will be two indirect memory loads plus an integer comparison.
You will be hard-pressed to beat this, efficiency-wise.

For comparison, if we were to provide custom type information (as we
do in XSD/e) it would be via virtual functions returning a static
string. In which case the same expression will cost you two virtual
function calls and an integer comparison.

Boris



More information about the xsd-users mailing list