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?
}
Watusimoto
  • 1,773
  • 1
  • 23
  • 38
  • I think it is evaluated at runtime. as type T is not know at compile time – someone_ smiley Sep 13 '13 at 08:41
  • 2
    What object would it use RTTI on? – CB Bailey Sep 13 '13 at 08:41
  • 2
    @someone_smiley: How would the compiler generate a correct specialization if it didn't know what `T` was? – CB Bailey Sep 13 '13 at 08:42
  • 4
    The "Notes" section on cpp reference says (http://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." – piwi Sep 13 '13 at 08:43
  • 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*. – Simple Sep 13 '13 at 08:47
  • 1
    @CharlesBailey: T has to be known at compile time, otherwise the program can't compile. – Watusimoto Sep 13 '13 at 08:49
  • 1
    @Watusimoto: but `T` is a type, not an object. (If you were responding to my first comment.) – CB Bailey Sep 13 '13 at 08:50
  • @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.) – Watusimoto Sep 13 '13 at 08:58
  • @piwi go and write an answer. – Walter Sep 13 '13 at 09:12
  • 2
    @Watusimoto: If you know that then I don't understand what question you are asking. – CB Bailey Sep 13 '13 at 09:18
  • @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. – Watusimoto Sep 13 '13 at 10:23
  • 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. – CB Bailey Sep 13 '13 at 10:44

2 Answers2

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
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.

piwi
  • 5,136
  • 2
  • 24
  • 48
  • In this instance `T` is a type, not an expression. – molbdnilo Sep 13 '13 at 09:18
  • 1
    @molbdnilo So the expression `typeid(T)` is resolved at compile time. (The compiler may still generate the call to `std::type_info::name` on it.) – James Kanze Sep 13 '13 at 09:22
  • @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. – molbdnilo Sep 13 '13 at 09:44
  • @molbdnilo 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. – James Kanze Sep 13 '13 at 10:04