命名空间
变体
操作

std::allocator_traits

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
allocator_traits
(C++11)
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
定义于头文件 <memory>
template< class Alloc >
struct allocator_traits;
(C++11 起)

allocator_traits 类模板提供了一种标准化的方式来访问 Allocator 的各种属性。标准容器和其他标准库组件通过此模板访问分配器,从而可以使用任何类类型作为分配器,只要用户提供的 std::allocator_traits 特化实现了所有必需的功能。

声明 std::allocator_traits 的显式或部分特化的程序是格式错误的,不需要诊断。

(C++23 起)

默认的、非特化的 std::allocator_traits 包含以下成员

目录

[编辑] 成员类型

类型 定义
allocator_type Alloc
value_type Alloc::value_type
pointer 如果存在,则为 Alloc::pointer,否则为 value_type*
const_pointer 如果存在,则为 Alloc::const_pointer,否则为 std::pointer_traits<pointer>::rebind<const value_type>
void_pointer 如果存在,则为 Alloc::void_pointer,否则为 std::pointer_traits<pointer>::rebind<void>
const_void_pointer 如果存在,则为 Alloc::const_void_pointer,否则为 std::pointer_traits<pointer>::rebind<const void>
difference_type 如果存在,则为 Alloc::difference_type,否则为 std::pointer_traits<pointer>::difference_type
size_type 如果存在,则为 Alloc::size_type,否则为 std::make_unsigned<difference_type>::type
propagate_on_container_copy_assignment 如果存在,则为 Alloc::propagate_on_container_copy_assignment,否则为 std::false_type
propagate_on_container_move_assignment 如果存在,则为 Alloc::propagate_on_container_move_assignment,否则为 std::false_type
propagate_on_container_swap 如果存在,则为 Alloc::propagate_on_container_swap,否则为 std::false_type
is_always_equal 如果存在,则为 Alloc::is_always_equal,否则为 std::is_empty<Alloc>::type

[编辑] 成员别名模板

类型 定义
rebind_alloc<T> 如果存在,则为 Alloc::rebind<T>::other,否则如果此 Alloc 形式为 SomeAllocator<U, Args>,则为 SomeAllocator<T, Args>,其中 Args 是零个或多个 类型参数
rebind_traits<T> std::allocator_traits<rebind_alloc<T>>

[编辑] 成员函数

[静态]
使用分配器分配未初始化的存储
(public static member function) [编辑]
[static] (C++23)
通过分配器分配至少请求大小的存储空间
(public static member function) [编辑]
[静态]
使用分配器释放存储
(public static member function) [编辑]
[静态]
在已分配的存储中构造一个对象
(function template) [编辑]
[静态]
销毁存储在已分配存储中的对象
(function template) [编辑]
[静态]
返回分配器支持的最大对象大小
(public static member function) [编辑]
获取复制标准容器后使用的分配器
(public static member function) [编辑]

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2108 C++11 无法显示分配器是无状态的 提供 is_always_equal

[编辑] 另请参阅

使用分配器的容器;相关特性(例如 propagate_on_container_swap)用法
(命名要求)
默认分配器
(类模板) [编辑]
为多层容器实现多层分配器
(类模板) [编辑]
提供有关类指针类型的信息
(类模板) [编辑]