Quote:
It will probably though convert the type even if you just wanted a check, thus be slower.
Not really. The check whether the conversion is allowed is expensive. Performing the conversion is just an afterthought. In fact, I'd wager the additional cost is actually zero on superscalar CPUs, as the necessary instructions are executed in parallel with whatever else is around them.
Quote:
The second checks if the types are the same, but doesn't check if one in inherited by the other.
Correct.
Quote:
In any case, C++ offers two things, so it should be more efficient.
Comparing typeids is typically one indirect access plus one comparison, but could also be three indirect accesses and a string comparison. Depends on the platform, the compiler, and the capabilities of the linker.
Doing a dynamic_cast is typically linear in the total number of direct and indirect base classes, doing most of the work of a typeid comparison at each step.
Quote:
And, as a final note, there isn't something EXACTLY like instanceof in C++
Well, given that dynamic_cast isn't significantly (or even measurably, probably) slower than a pure type check, I'd say my function is exactly like instanceof.