Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to #include <typeinfo> when using the typeid operator?

Tags:

c++

typeid

rtti

The typeid represents a C++ RTTI operator being also a C++ keyword. It returns a std::type_info object that holds (dynamic) type specific information.

From what I understood from various sources, one MUST include <typeinfo> when using typeid, otherwise the program is ill-formed. In fact, my gcc5.2 compiler doesn't even compile the program if I don't include the before-mentioned header. I don't understand why is a header inclusion mandated for the usage of a C++ keyword. I understand mandating a header for whenever we use some object declared/defined in that header, but typeid is not of a class type. So what is the reason behind this enforcement of including the header <typeinfo>?

like image 200
vsoftco Avatar asked Nov 14 '15 04:11

vsoftco


People also ask

Why do I have to or need to?

If something is a requirement in order to do something else, then you use 'need to'. If it is something you are obligated to do regardless of any further goals, then you use 'have to'.

Did I need or do I need?

“Why do I need this” is a question of the present, it is current, therefore a question of what I need to understand now, the present tense. “Why did I need this” is a question of what has happened previously, in the past; something that has already accurred, so is past tense.

Is it needed to or needed too?

Two verbs in English Notice that the first verbs above(want, need) use the infinitive form for the second verb(to go, to bring). For this reason, we use “need to” and not “need too”.

Have to or need to meaning?

The verbs need, have to, and must are all synonyms of one another and are used to mean that something is necessary or required.


1 Answers

The next paragraph:

The typeid expression is lvalue expression which refers to an object with static storage duration, of the polymorphic type const std::type_info or of some type derived from it.

Because it is an lvalue expression, which uses reference initialization to declare an initializer of std::type_info. <typeinfo> contains the definition for that object.

like image 189
mattkgross Avatar answered Sep 27 '22 20:09

mattkgross