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

Klaim - Joël Lamotte mjklaim at gmail.com
Tue Nov 29 17:09:01 EST 2011


2011/11/29 Boris Kolpackov <boris at codesynthesis.com>

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


As I said I know about this solution but providing a custom id generated at
compile time is still less expensive because you don't require the user to
use RTTI.
In my case I'm already using RTTI so I don't really care but I just wanted
to check that you didn't provide something already.

Thanks for the suggestion anyway.

Joël Lamotte


More information about the xsd-users mailing list