`operator new` 是 C++ 核心语言的一部分吗?

Is `operator new` a part of C++ core language?

有人告诉我 "new-expression will call operator new to manage dynamic storage and initialize the object at the same time" 很多次了。我不怀疑这一点。但我想知道,既然 operator new 是在标准库头文件 <new> 中声明的,那么即使我们不包含头文件,我们怎么还能使用 new 表达式呢?

operator new 是 C++ 核心语言的一部分还是编译器隐式包含 <new>

是的,operator new 是 C++ 标准的一部分。

参见 [expr.new]

#include <new> 的大部分是 implicitly declared。但是 #include <new> 声明了一些 operator new 的更多版本,它们不是隐式的,例如 placement new*:

void* operator new  (std::size_t size, void* ptr) noexcept;
void* operator new[](std::size_t size, void* ptr) noexcept;

* placement new 不建议日常使用。

header 仅提供全局 operator new 的所有变体的声明。只要您获得正确的操作员签名,您仍然可以在不包括 header 的情况下覆盖它们。

operator new 是标准的一部分。该实现需要在程序中的每个翻译单元的全局范围内提供运算符。来自 [basic.stc.dynamic]/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.4.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);
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
void operator delete(void*, std::size_t) noexcept;
void operator delete[](void*, std::size_t) noexcept;

强调我的

这就是为什么您无需包含任何内容即可使用 new/new[]delete/delete[]

默认分配和释放函数是标准库的特殊组件;它们具有以下独特的属性:

Global: All three versions of operator new are declared in the global namespace, not within the std namespace.
Implicit: The allocating versions ((1) and (2)) are implicitly declared in every translation unit of a C++ program, no matter whether header is included or not.
Replaceable: The allocating versions ((1) and (2)) are also replaceable: A program may provide its own definition that replaces the one provided by default to produce the result described above, or can overload it for specific types.

来源:http://www.cplusplus.com/reference/new/operator%20new/

我同意,这往往令人困惑,但 C++ 中有很多新闻(双关语):

  1. 其中之一是“new 运算符”
  2. 另一个是"operator new"一个执行实际内存分配的函数
  3. 有位置new
  4. 有些人可能不喜欢这个名字,但也有数组 news (new[])

不考虑 3、4,因为它们值得自己 post (What uses are there for "placement new"? and How do I use arrays in C++?), the first one ("new operator") resides in the global namespace, and it is accessible with the the scope-resolution operator (::), and the second one is the one you can overload in your classes. However regarding the relation of these two, there is an excellent explanation in MSDN (https://msdn.microsoft.com/en-us/library/kewsb8ba.aspx) 我只是把它粘贴在这里:

The new operator invokes the function operator new. For arrays of any type, and for objects that are not of class, struct, or union types, a global function, ::operator new, is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis. When the compiler encounters the new operator to allocate an object of type type, it issues a call to type::operator new( sizeof( type ) ) or, if no user-defined operator new is defined, ::operator new( sizeof( type ) ). Therefore, the new operator can allocate the correct amount of memory for the object.