[英]Does typeid(T) get evaluated at run time or compile time?
I cannot find the answer to this seemingly simple question anywhere. 我无法在任何地方找到这个看似简单的问题的答案。
Does the following C++ function use RTTI? 以下C ++函数是否使用RTTI? It certainly doesn't have to, but I was wondering if there is a guarantee that typeid will be determined at compile time.
它当然不必,但我想知道是否有保证在编译时确定typeid。
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. 由于
typeid
被施加到一个类型,而不是一个对象, 没有运行时类型信息,从而开销将不会是一个问题。
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. 我无法想象有人会编写一个在编译时没有评估
typeid(T)
的编译器。
As I mentioned in a comment, the "Notes" section regarding typeid()
on cpp reference says: 正如我在评论中提到的,关于cpp引用上的
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.
当应用于多态类型的表达式时,对typeid表达式的求值可能涉及运行时开销(虚拟表查找),否则在编译时解析typeid表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.