# 标准库头文件 <new>

< cpp‎ | header

此头文件是动态内存管理库的一部分，特别提供低层内存管理特性。

### 类
bad_alloc | 内存分配失败时抛出的异常 (类)
bad_array_new_length (C++11) | 在分配有非法长度的数组时抛出的异常 (类)
nothrow_t | 用于选择不抛出分配函数的标签类型 (类)
align_val_t (C++17) | 用于向带对齐分配及解分配函数传递对齐值的类型 (枚举)
destroying_delete_t (C++20) | 用于鉴别 operator delete 的销毁删除重载的标签类型 (类)
### 类型
new_handler | new 处理函数的函数指针类型 (typedef)
### 常量
nothrow | 用于选择不抛出分配函数的 `nothrow_t` 类型对象 (常量)
hardware_destructive_interference_size hardware_constructive_interference_size (C++17) | 避免假共享的最小偏移 促使真共享的最大偏移 (常量)
### 函数
operator new operator new[] | 分配函数 (函数)
operator delete operator delete[] | 解分配函数 (函数)
get_new_handler (C++11) | 获得当前的 new 处理函数 (函数)
set_new_handler | 注册一个 new 处理函数 (函数)
launder (C++17) | 指针优化屏障 (函数模板)

### 概要

    namespace std {
      // 存储分配错误
      class bad_alloc;
      class bad_array_new_length;

      struct destroying_delete_t {
        explicit destroying_delete_t() = default;
      };
      inline constexpr destroying_delete_t destroying_delete{};

      // 全局 operator new 控制
      enum class align_val_t : size_t {};

      struct nothrow_t { explicit nothrow_t() = default; };
      extern const nothrow_t nothrow;

      using new_handler = void (*)();
      new_handler get_new_handler() noexcept;
      new_handler set_new_handler(new_handler new_p) noexcept;

      // 指针优化屏障
      template<class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;

      // 硬件干涉大小
      inline constexpr size_t hardware_destructive_interference_size = /* 由实现定义 */;
      inline constexpr size_t hardware_constructive_interference_size = /* 由实现定义 */;
    }

    // 存储分配与解分配
    [[nodiscard]] void* operator new(std::size_t size);
    [[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment);
    [[nodiscard]] void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
    [[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment,
                                     const std::nothrow_t&) noexcept;

    void  operator delete(void* ptr) noexcept;
    void  operator delete(void* ptr, std::size_t size) noexcept;
    void  operator delete(void* ptr, std::align_val_t alignment) noexcept;
    void  operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
    void  operator delete(void* ptr, const std::nothrow_t&) noexcept;
    void  operator delete(void* ptr, std::align_val_t alignment,
                          const std::nothrow_t&) noexcept;

    [[nodiscard]] void* operator new[](std::size_t size);
    [[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment);
    [[nodiscard]] void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
    [[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment,
                                       const std::nothrow_t&) noexcept;

    void  operator delete[](void* ptr) noexcept;
    void  operator delete[](void* ptr, std::size_t size) noexcept;
    void  operator delete[](void* ptr, std::align_val_t alignment) noexcept;
    void  operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
    void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;
    void  operator delete[](void* ptr, std::align_val_t alignment,
                            const std::nothrow_t&) noexcept;

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