Setting aside all the concerns about the necessity of using typeid and dynamic_cast and their questionable effects on code maintenance, is there any information about the performance of these two dynamic type introspection mechanisms? The Wikipedia article on RTTI claims that:
The use of typeid, in a non-polymorphic context, is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid is always a constant-time procedure, whereas dynamic_cast may need to traverse the class derivation lattice of its argument at runtime.
However no citation is provided. As such I wanted to ask if this claim is true and if one of these two mechanisms should definitely be preferred performance-wise over the other. I wasn't able to find information about any bounds on time complexity of these operations.
typeid, you can check for a specific type but not whether the type is compatible (like you could withdynamic_cast): Counter Example on Compiler Explorer - Scheff's Cattype_infodoesn't really have to be that cheap. This question speaks of implementations that usestrcmp: stackoverflow.com/questions/49773686/… . This libstdc++ file seems to confirm: gcc.gnu.org/onlinedocs/gcc-7.5.0/libstdc++/api/… . - janekb04