Does typeid(T) get evaluated at run time or compile time?
Asked Answered
S

2

7

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?
}
Scrivenor answered 13/9, 2013 at 8:37 Comment(12)
I think it is evaluated at runtime. as type T is not know at compile timeBini
What object would it use RTTI on?Octet
@someone_smiley: How would the compiler generate a correct specialization if it didn't know what T was?Octet
The "Notes" section on cpp reference says (en.cppreference.com/w/cpp/language/typeid): "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."Quittance
I imagine it could be evaluated at compile-time by the compiler in the case of typeid(T) or typeid(x) where the static type of x is a concrete class, but it is never a constant-expression.Relationship
@CharlesBailey: T has to be known at compile time, otherwise the program can't compile.Scrivenor
@Watusimoto: but T is a type, not an object. (If you were responding to my first comment.)Octet
@CharlesBailey: Yes; the compiler knows all the classes that getName() can be called with, and it "detemplatifies" the code and creates a getName() function for each type. It does not do this dynamically at run time. (And typeid works on types as well as objects.)Scrivenor
@Quittance go and write an answer.Antony
@Watusimoto: If you know that then I don't understand what question you are asking.Octet
@CharlesBailey: The question states that the typeid can be determined at compile time, but I was asking if it actually was guaranteed to do so because all the documentation I have seen on the matter is vague on this point. It focuses instead on the need to use RTTI to determine type of a pointer at runtime, leaving my question unanswered. My immediate motivation was that one of my colleagues thought I was incurring additional overhead with a function like this, and I was having a hard time disproving him.Scrivenor
Why not scrub the reference to RTTI entirely from the question, it's just causing confusion. It's not relevant to you. There is no object involved in your use of typeid so there can be no RTTI. Instead just ask if "typeid( typeid )" is resolved at compile time.Octet
D
9

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.

Donar answered 13/9, 2013 at 9:17 Comment(0)
Q
7

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.

Quittance answered 13/9, 2013 at 9:14 Comment(4)
In this instance T is a type, not an expression.Donar
@Donar So the expression typeid(T) is resolved at compile time. (The compiler may still generate the call to std::type_info::name on it.)Zambia
@JamesKanze Shame on me for not reading the whole sentence... I still can't find any reference to compile-time resolution in The Standard, though.Donar
@Donar I thought that that was what he was quoting. You're right; the standard doesn't make any guarantee (although it's hard to see how it could be otherwise when you use typeid with a type name). Still, it's an error in his source.Zambia

© 2022 - 2024 — McMap. All rights reserved.