C++ 中何时需要#include <new> 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2788388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is #include <new> library required in C++?
提问by Paul Caheny
According to this reference entry for operator new ( http://www.cplusplus.com/reference/std/new/operator%20new/) :
根据 operator new ( http://www.cplusplus.com/reference/std/new/operator%20new/) 的参考条目:
Global dynamic storage operator functions are special in the standard library:
- All three versions of operator new are declared in the global namespace, not in the std namespace.
- The first and second versions are implicitly declared in every translation unit of a C++ program: The header does not need to be included for them to be present.
全局动态存储操作符函数在标准库中比较特殊:
- operator new 的所有三个版本都在全局命名空间中声明,而不是在 std 命名空间中。
- 第一个和第二个版本在 C++ 程序的每个翻译单元中隐式声明:不需要包含头文件就可以显示它们。
This seems to me to imply that the third version of operator new (placement new) is not implicitly declared in every translation unit of a C++ program and the header <new>
does need to be included for it to be present. Is that correct?
在我看来,这意味着在 C++ 程序的每个翻译单元中都没有隐式声明 operator new (placement new) 的第三个版本,并且<new>
确实需要包含头文件才能存在。那是对的吗?
If so, how is it that using both g++ and MS VC++ Express compilers it seems I can compile code using the third version of new without #include <new>
in my source code?
如果是这样,如何使用 g++ 和 MS VC++ Express 编译器似乎我可以使用第三版 new 编译代码而无需#include <new>
在我的源代码中?
Also, the MSDN Standard C++ Library reference entry on operator new gives some example code for the three forms of operator new which contains the #include <new>
statement, however the example seems to compile and run just the same for me without this include?
此外,关于 operator new 的 MSDN 标准 C++ 库参考条目提供了包含该#include <new>
语句的三种形式的 operator new 的一些示例代码,但是该示例似乎对我来说编译和运行时没有这个包含?
// new_op_new.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass( )
{
cout << "Construction MyClass." << this << endl;
};
~MyClass( )
{
imember = 0; cout << "Destructing MyClass." << this << endl;
};
int imember;
};
int main( )
{
// The first form of new delete
MyClass* fPtr = new MyClass;
delete fPtr;
// The second form of new delete
char x[sizeof( MyClass )];
MyClass* fPtr2 = new( &x[0] ) MyClass;
fPtr2 -> ~MyClass();
cout << "The address of x[0] is : " << ( void* )&x[0] << endl;
// The third form of new delete
MyClass* fPtr3 = new( nothrow ) MyClass;
delete fPtr3;
}
Could anyone shed some light on this and when and why you might need to #include <new>
- maybe some example code that will not compile without #include <new>
?
任何人都可以对此有所了解,以及何时以及为什么您可能需要#include <new>
- 也许一些示例代码没有就无法编译#include <new>
?
Thanks.
谢谢。
采纳答案by Abhay
Nothing in C++ prevents standard headers from including other standard headers. So if you include anystandard header you might conceivably indirectly include allof 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.
C++ 中没有任何内容阻止标准头文件包含其他标准头文件。因此,如果您包含任何标准标头,您可能会间接包含所有这些标头。但是,这种行为完全取决于实现,如果您需要特定标头的功能,您应该始终自己明确地包含它。
回答by Abhay
The C++ Standard verse 3.7.4.2says :-
C++ 标准第3.7.4.2节说:-
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.
该库为全局分配和解除分配函数提供了默认定义。一些全局分配和释放函数是可替换的 (18.6.1)。C++ 程序最多应提供一个可替换分配或释放函数的定义。任何此类函数定义都会替换库 (17.6.3.6) 中提供的默认版本。以下分配和释放函数 (18.6) 在程序的每个翻译单元的全局范围内隐式声明。
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 ]
这些隐式声明仅引入了函数名 operator new、operator new[]、operator delete、operator delete[]。[ 注意:隐式声明不引入名称 std、std::bad_alloc 和 std::size_t,或库用于声明这些名称的任何其他名称。因此,引用这些函数之一而不包含标题的 newexpression、delete-expression 或函数调用是格式良好的。然而,引用 std、std::bad_alloc 和 std::size_t 是格式错误的,除非通过包含适当的头文件来声明名称。——尾注]
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.
此外, 的std::nothrow
版本operator new
要求包含标头。尽管该标准没有指定在其他头文件中隐式包含头文件。因此,std::bad_alloc
在引用名称等时遵循标准是安全且可移植的。
回答by Cheers and hth. - Alf
Regarding the question in the title,
关于标题中的问题,
”When is
#include <new>
library required in C++?
”当
#include <new>
用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.
关键字new
可以以多种方式使用。普通使用不需要包含任何标题。但是,使用此关键字的一种可能方法是调用由<new>
标头定义的特定“placement new”函数。使用这种用法,您需要直接或间接包含<new>
标题。除非您需要,否则不要包含该标题或任何其他标题;默认情况下不包含标题。另一方面,不要依赖包含另一个头文件的实现特定版本:始终根据标准(或其他)规范提供的内容包含您需要的内容。
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.
在 C++ 中,标准库头文件允许包含其他标准库头文件(或其他标准库头文件提供的内容),由实现决定。
回答by el.pescado
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
无法分配内存时,new
在<new>
标头中定义的运算符会引发bad_alloc
异常(在同一标头中声明)而不是返回 NULL。<new>
标头还定义了
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 . All three operator overloads:<new>
you get only plain old, NULL-returning operator new
不抛出异常并放置新变体的变体。 如果没有. 所有三个运算符重载:<new>
你,你只会变得很老,返回 NULLoperator new
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.
在<new>
头文件中声明。但是,某些编译器可能会隐式地使它们可用,但这是非标准的,您不应依赖它。