Re: Cost of dynamic_cast
Alex Blekhman wrote:
"Tom Widmer [VC++ MVP]" wrote:
On Windows, it is possible to have multiple copies of the
RTTI of a class due to the fact that the same class can be
compiled into multiple modules of a single process (e.g.
it might be in the .exe and in 3 .dlls). As a result,
dynamic_cast actually performs string comparisons against
fully qualified class names (or some kind of mangled
version) to work out if two typeids actually match.
dynamic_cast has to check whether the cast-to class is a
base of the dynamic type of the object, so it potentially
has to perform a series of these comparisons.
I was under impression that VC++ implementation of
dynamic_cast first tries to compare RTTI pointers and only
if they don't match starts string comparison of types. So,
if both cast-from and cast-to RTTI's happen to be within the
same module, then dynamic_cast is mere comparison of
integers. However, I never read anything official about
this, so I may be mistaken.
Still, any failed dynamic_cast is going to involve at least one string
comparison, I think. But I assume the code has a fast path for when the
pointers are equal. e.g. something like
bool type_info::operator==(typeinfo const& other) const
{
return this == &other || strcmp(name(), other.name()) == 0;
}
Tom