I've read the following two earlier questions,but still can't have a good undertanding.
1. Why do I need to #include when using the typeid operator?
2. When is #include library required in C++?
Since the typeid
uses the type_info
class,it's reasonable to require us #include<typeinfo>
.
However,the new operators
also uses std::bad_alloc
,why does it not require us to #include <new>
? ( I know that sizeof()
does not require <cstddef>
because the size_t
coulde be replaced with built-in types during compile time,as long as the compiler knows what the size_t
actually is. )
According to the most-voted answer in the 2th question,he says:
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 newexpression, 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.
The paragraph above is confusing,since everytime we use a type,we need to declare a type before usage like class A;void foo(A*);
And the implicit declaration of void* operator new(std::size_t) throw(std::bad_alloc);
also has a type std::bad_alloc
,is it a privilege for the implicit declaration to not declare the type it use?