跳到主要内容

std::allocator_traits

定义于内存
自 C++11 起
template< class Alloc >
struct allocator_traits;

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

声明 std::allocator_traits 的显式或部分特化的程序是病态的,不需要诊断。 (自 C++23 起)

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

成员类型

pub类型定义
puballocator_typeAlloc
pubvalue_typeAlloc::value_type
pubpointer如果存在 Alloc::pointer,否则为 value_type*
pubconst_pointer如果存在 Alloc::const_pointer,否则为
std::pointer_traits <pointer>::rebind<const value_type>
pubvoid_pointer如果存在 Alloc::void_pointer,否则为
std::pointer_traits <pointer>::rebind<void>
pubconst_void_pointer如果存在 Alloc::const_void_pointer,否则为
std::pointer_traits <pointer>::rebind<const_void>
pubdifference_type如果存在 Alloc::difference_type,否则为
std::pointer_traits <pointer>::difference_type
pubsize_type如果存在 Alloc::size_type,否则为
std::make_unsigned <difference_type>::type
pubpropagate_on_container_copy_assignment如果存在 Alloc::propagate_on_container_copy_assignment,否则为
std::false_type
pubpropagate_on_container_move_assignment如果存在 Alloc::propagate_on_container_move_assignment,否则为
std::false_type
pubpropagate_on_container_swap如果存在 Alloc::propagate_on_container_swap,否则为
std::false_type
pubis_always_equal如果存在 Alloc::is_always_equal,否则为
std::is_empty <Alloc>::type

成员别名模板

pub类型定义
pubrebind_alloc<T>如果存在 Alloc::rebind<T>::other,否则如果此 Alloc 是 Alloc<U, Args>,则为 Alloc<T, Args>
pubrebind_traits<T>std::allocator_traits<rebind_alloc<T>>

成员函数

puballocatestatic使用分配器分配未初始化的存储
puballocate_at_least(C++23)static使用分配器分配未初始化的存储
pubdeallocatestatic使用分配器解除分配存储
pubconstructstatic在已分配的存储中构造对象
pubdestroystatic销毁存储在已分配存储中的对象
pubmax_sizestatic返回分配器支持的最大对象大小
pubselect_on_container_copy_constructionstatic获取复制标准容器后要使用的分配器

缺陷报告

以下改变行为的缺陷报告已追溯应用于先前发布的 C++ 标准。

DR应用于发布时的行为正确行为
LWG 2108C++11没有办法表明分配器是无状态的提供了 is_always_equal

std::allocator_traits

定义于内存
自 C++11 起
template< class Alloc >
struct allocator_traits;

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

声明 std::allocator_traits 的显式或部分特化的程序是病态的,不需要诊断。 (自 C++23 起)

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

成员类型

pub类型定义
puballocator_typeAlloc
pubvalue_typeAlloc::value_type
pubpointer如果存在 Alloc::pointer,否则为 value_type*
pubconst_pointer如果存在 Alloc::const_pointer,否则为
std::pointer_traits <pointer>::rebind<const value_type>
pubvoid_pointer如果存在 Alloc::void_pointer,否则为
std::pointer_traits <pointer>::rebind<void>
pubconst_void_pointer如果存在 Alloc::const_void_pointer,否则为
std::pointer_traits <pointer>::rebind<const_void>
pubdifference_type如果存在 Alloc::difference_type,否则为
std::pointer_traits <pointer>::difference_type
pubsize_type如果存在 Alloc::size_type,否则为
std::make_unsigned <difference_type>::type
pubpropagate_on_container_copy_assignment如果存在 Alloc::propagate_on_container_copy_assignment,否则为
std::false_type
pubpropagate_on_container_move_assignment如果存在 Alloc::propagate_on_container_move_assignment,否则为
std::false_type
pubpropagate_on_container_swap如果存在 Alloc::propagate_on_container_swap,否则为
std::false_type
pubis_always_equal如果存在 Alloc::is_always_equal,否则为
std::is_empty <Alloc>::type

成员别名模板

pub类型定义
pubrebind_alloc<T>如果存在 Alloc::rebind<T>::other,否则如果此 Alloc 是 Alloc<U, Args>,则为 Alloc<T, Args>
pubrebind_traits<T>std::allocator_traits<rebind_alloc<T>>

成员函数

puballocatestatic使用分配器分配未初始化的存储
puballocate_at_least(C++23)static使用分配器分配未初始化的存储
pubdeallocatestatic使用分配器解除分配存储
pubconstructstatic在已分配的存储中构造对象
pubdestroystatic销毁存储在已分配存储中的对象
pubmax_sizestatic返回分配器支持的最大对象大小
pubselect_on_container_copy_constructionstatic获取复制标准容器后要使用的分配器

缺陷报告

以下改变行为的缺陷报告已追溯应用于先前发布的 C++ 标准。

DR应用于发布时的行为正确行为
LWG 2108C++11没有办法表明分配器是无状态的提供了 is_always_equal