I cannot find the answer to this seemingly simple question anywhere.
Does the following C++ function use RTTI? It certainly doesn't have to, but I was wondering if there is a guarantee that typeid will be determined at compile time.
template <typename T>
const char *getName()
{
return typeid(T).name(); // Resolved at compile time?
}
Since typeid
is applied to a type rather than an object, there is no runtime type information, so that overhead won't be a problem.
On the other hand: as far as I can see, the standard makes no requirements regarding when the value will be determined, so there's no guarantee that there's no runtime overhead.
Edit:
Of course, the fact that there's (possibly) no guarantee doesn't mean that it's not a reasonable assumption.
I can't imagine that anyone would write a compiler that didn't evaluate typeid(T)
at compile time.
As I mentioned in a comment, the "Notes" section regarding typeid()
on cpp reference says:
When applied to an expression of polymorphic type, evaluation of a typeid expression may involve runtime overhead (a virtual table lookup), otherwise typeid expression is resolved at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With