When is #include <new> library required in C++?

ぃ、小莉子 提交于 2019-11-27 19:16:17

Nothing in C++ prevents standard headers from including other standard headers. So if you include any standard header you might conceivably indirectly include all of them. However, this behaviour is totally implementation dependent, and if you need the features of a specific header you should always explicitly include it yourself.

The C++ Standard verse 3.7.4.2 says :-

The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.6.1). A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library (17.6.3.6). The following allocation and deallocation functions (18.6) are implicitly declared in global scope in each translation unit of a program.

void* operator new(std::size_t) throw(std::bad_alloc); 
void* operator new[](std::size_t) throw std::bad_alloc); 
void operator delete(void*) throw(); 
void operator delete[](void*) throw();

These implicit declarations introduce only the function names operator new, operator new[], operator delete, operator delete[]. [ 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. —end note ]

Also, the std::nothrow version of the operator new requires the inclusion of the header. The standard though does not specify implicit inclusion of the header files within other header files. So it is safe and portable to follow the standard when the names std::bad_alloc etc are referred.

Regarding the question in the title,

When is #include <new> library required in C++?

The keyword new can be used in various ways. Ordinary use does not require inclusion of any headers. But one possible way to use this keyword is to invoke the particular “placement new” function defined by the <new> header. With that usage you need to directly or indirectly include the <new> header. Do not include that header, or any other header, unless you need it; do not include headers by default. On the other hand, do not rely on an implementation specific version of a header including another one: always include what you need as per the standard's (or other) specifications of what they provide.

Regarding the question in the body,

the example seems to compile and run just the same for me without this include?

In C++ standard library headers are permitted to include other standard library headers (or the stuff provided by other standard library headers), at the implementation's discretion.

Operator new defined in <new> header throws bad_alloc exception (which is declared in the same header) instead of returning NULL when memory allocation is not possible. <new> header also defines

void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();

variant which does not throw exceptions and placement new variant. Without <new> you get only plain old, NULL-returning operator new. All three operator overloads:

void* operator new (std::size_t size) throw (std::bad_alloc);
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();
void* operator new (std::size_t size, void* ptr) throw();

are declared in <new> header. However, some compilers may make them available implicitly, but this is non-standard, and you should not rely on it.

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