std::scoped_allocator_adaptor
| 定义于头文件  <scoped_allocator> | ||
| template< class OuterAlloc, class... InnerAllocs > class scoped_allocator_adaptor | (C++11 起) | |
std::scoped_allocator_adaptor 类模板是一个分配器,可用于多级容器(`vector` 的 `set` 的 `list` 的 `tuple` 的 `map` 等)。它实例化一个外部分配器类型 OuterAlloc 和零个或多个内部分配器类型 InnerAlloc...。直接用 scoped_allocator_adaptor 构造的容器使用 OuterAlloc 来分配其元素,但如果元素本身是一个容器,它则使用第一个内部分配器。该容器的元素,如果它们本身是容器,则使用第二个内部分配器,以此类推。如果容器的层级多于内部分配器的数量,则最后一个内部分配器将重复用于所有进一步的嵌套容器。
此适配器的目的是正确初始化嵌套容器中的有状态分配器,例如当嵌套容器的所有级别都必须放置在同一个共享内存段中时。适配器的构造函数接受列表中所有分配器的参数,每个嵌套容器根据需要从适配器获取其分配器的状态。
为了 scoped_allocator_adaptor 的目的,如果下一个内部分配器是 A,则任何类 T,对于其 std::uses_allocator<T,A>::value == true,都会像容器一样参与递归。此外,std::pair 被 scoped_allocator_adaptor::construct 的特定重载视为此类容器。
典型的实现将 std::scoped_allocator_adaptor<InnerAllocs...> 的实例作为成员对象持有。
请注意,std::pmr::polymorphic_allocator 遵循使用分配器构造传播到嵌套容器,并且不需要(也无法与)std::scoped_allocator_adaptor 配合使用。
| 目录 | 
[编辑] 嵌套类型
| 类型 | 定义 | 
| outer_allocator_type | OuterAlloc | 
| inner_allocator_type | 
 | 
| value_type | std::allocator_traits<OuterAlloc>::value_type | 
| size_type | std::allocator_traits<OuterAlloc>::size_type | 
| difference_type | std::allocator_traits<OuterAlloc>::difference_type | 
| pointer | std::allocator_traits<OuterAlloc>::pointer | 
| const_pointer | std::allocator_traits<OuterAlloc>::const_pointer | 
| void_pointer | std::allocator_traits<OuterAlloc>::void_pointer | 
| const_void_pointer | std::allocator_traits<OuterAlloc>::const_void_pointer | 
给定 OuterAlloc 和 InnerAlloc... 为 Allocs 集合
| 类型 | 定义 | 
| propagate_on_container_copy_assignment | 
 | 
| propagate_on_container_move_assignment | 
 | 
| propagate_on_container_swap | 
 | 
| is_always_equal | 
 | 
[编辑] 成员函数
| 创建一个新的 scoped_allocator_adaptor对象(公共成员函数) | |
| 销毁 scoped_allocator_adaptor对象(公共成员函数) | |
| 赋值 scoped_allocator_adaptor(公共成员函数) | |
| 获取 inner_allocator引用(公共成员函数) | |
| 获取 outer_allocator引用(公共成员函数) | |
| 使用外部分配器分配未初始化的存储 (公共成员函数) | |
| 使用外部分配器解除分配存储 (公共成员函数) | |
| 返回外部分配器支持的最大分配大小 (公共成员函数) | |
| 在已分配的存储中构造对象,如果合适则将内部分配器传递给其构造函数 (公共成员函数) | |
| 在已分配的存储中销毁对象 (公共成员函数) | |
| 复制 scoped_allocator_adaptor及其所有分配器的状态(公共成员函数) | |
| 仅用于说明的函数模板 | |
| 获取最外部的分配器 (仅用于说明的成员函数*) | |
| 使用最外部的分配器构造对象 (仅用于说明的成员函数*) | |
| 使用最外部的分配器销毁对象 (仅用于说明的成员函数*) | |
[编辑] 非成员函数
| (在 C++20 中移除) | 比较两个 scoped_allocator_adaptor对象(函数模板) | 
[编辑] 推导指南(C++17 起)
[编辑] 嵌套类
| 类 | 定义 | 
| rebind | template< class T > struct rebind | 
[编辑] 示例
#include <boost/interprocess/allocators/adaptive_pool.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <scoped_allocator> #include <vector> namespace bi = boost::interprocess; template<class T> using alloc = bi::adaptive_pool<T, bi::managed_shared_memory::segment_manager>; using ipc_row = std::vector<int, alloc<int>>; using ipc_matrix = std::vector<ipc_row, std::scoped_allocator_adaptor<alloc<ipc_row>>>; int main() { bi::managed_shared_memory s(bi::create_only, "Demo", 65536); // create vector of vectors in shared memory ipc_matrix v(s.get_segment_manager()); // for all these additions, the inner vectors obtain their allocator arguments // from the outer vector's scoped_allocator_adaptor v.resize(1); v[0].push_back(1); v.emplace_back(2); std::vector<int> local_row = {1, 2, 3}; v.emplace_back(local_row.begin(), local_row.end()); bi::shared_memory_object::remove("Demo"); }
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 | 
|---|---|---|---|
| LWG 2108 | C++11 | 没有办法表明 scoped_allocator_adaptor是否是无状态的 | 提供了 is_always_equal | 
[编辑] 另见
| (C++11) | 提供关于分配器类型的信息 (类模板) | 
| (C++11) | 检查指定类型是否支持 uses-allocator 构造 (类模板) | 
| 默认分配器 (类模板) | 


