C++ Type Casting safety?

Started by
11 comments, last by Nitage 18 years, 1 month ago
Quote:Original post by blaze02
RTTI... ick... so... slow.

Its not like it does any optimizations, it just embeds a lot of junk in the code so it knows which object is of which type.


dynamic_cast only needs to traverse the type lattice to determine if an object can be converted to a given type, using vtbl information. RTTI will have no impact outside calls to typeid and dynamic_cast. On simple lattice traversals (the kind you can do with enumerations) it will probably be even faster than an equivalent hand-crafted system. On more complex lattice traversals, comparison is impossible since enumeration-based methods cannot perform them.

It uses less memory, too: in a program with 10 classes and an average of 1000 objects, the enumeration method adds 4000 bytes of overhead, while the RTTI method adds a few hundred cache-friendly bytes.

Advertisement
Apart from significant increase in exe size I kinda doubt you would see significant performance loss without over-using dynamic cast and such.
Not to mention that the extra information the compiler inserts for RTTI is required for exception handling - 2 for the price of 1.

Having said that, I always have RTTI disabled in release builds anyway.

This topic is closed to new replies.

Advertisement