Is typeid (T) checked at runtime or compile time?

I can not find the answer to this seemingly simple question anywhere.

Does the following function use C ++ RTTI? This, of course, is not necessary, but I was wondering if there is a guarantee that the typeid will be determined at compile time.

template <typename T> const char *getName() { return typeid(T).name(); // Resolved at compile time? } 
+6
source share
2 answers

Since typeid is applied to the type, and not to the object, there is no information about the type of runtime, so overhead will not be a problem.

On the other hand: as far as I can tell, there are no requirements in the standard as to when the value will be determined, so there is no guarantee that the overhead will not be met.


Edit:
Of course, the fact that there is (possibly) no guarantee does not mean that this is not a reasonable assumption.
I cannot imagine anyone writing a compiler that did not evaluate typeid(T) at compile time.

+8
source

As I mentioned in a comment, the β€œNotes” section regarding typeid() in the cpp link says:

When applied to an expression of a polymorphic type, the evaluation of the typeid expression may include runtime overhead (virtual table lookup), otherwise the typeid expression is allowed at compile time.

+5
source

Source: https://habr.com/ru/post/953758/


All Articles