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

荒凉一梦 提交于 2019-11-29 13:14:52

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.

Abdul Rehman

typeid is not the only one that needs header

new also requires header <new> in some cases

Note: the implicit declarations do not introduce the names std, std::bad_alloc, and std::size_t, or any other names that the library uses to declare these names. Thus, a new-expression, delete-expression or function call that refers to one of these functions without including the header is well-formed. However, referring to std, std::bad_alloc, and std::size_t is ill-formed unless the name has been declared by including the appropriate header. —end note

See abhay's answer on new keyword

Another operator sizeof which returns std::size_t ( It does not actually need to include header, but my point here is that it uses an alias which is also defined in a header)

C++ §5.3.3

The result of sizeof and sizeof... is a constant of type std::size_t. [Note: std::size_t is defined in the standard header <cstddef>(18.2).— end note]

typeid use classes which are declared in <typeinfo> header

Header <typeinfo> synopsis

namespace std {
class type_info;
class bad_cast;
class bad_typeid;
}

See section 18.7 on iso cpp paper

IMO, Its C++ Standard Design Techniques, to keep the compiler neat, clean and lightweight

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!