std::scoped_allocator_adaptor
定义于头文件 <scoped_allocator> |
||
template< class OuterAlloc, class... InnerAllocs > class scoped_allocator_adaptor |
(C++11 起) | |
std::scoped_allocator_adaptor
类模板是一种分配器,可用于多层容器(例如,向量的集合的列表的元组的映射等)。它使用一个外部分配器类型 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 遵循uses-allocator 构造传播到嵌套容器,并且不需要(也不适用于) std::scoped_allocator_adaptor
。
内容 |
[edit] 嵌套类型
类型 | 定义 |
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
|
|
[edit] 成员函数
创建一个新的 scoped_allocator_adaptor 对象(公有成员函数) | |
析构一个 scoped_allocator_adaptor 对象(公有成员函数) | |
赋值一个 scoped_allocator_adaptor (公有成员函数) | |
获取一个 inner_allocator 引用(公有成员函数) | |
获取一个 outer_allocator 引用(公有成员函数) | |
使用外部分配器分配未初始化的存储 (公有成员函数) | |
使用外部分配器释放存储 (公有成员函数) | |
返回外部分配器支持的最大分配大小 (公有成员函数) | |
在已分配的存储中构造对象,并在适当的情况下将内部分配器传递给其构造函数 (公有成员函数) | |
destroy (公有成员函数) | |
复制 scoped_allocator_adaptor 及其所有分配器的状态(公有成员函数) | |
仅为阐述目的的函数模板 | |
获取最外层分配器 (仅为阐述目的的成员函数*) | |
使用最外层分配器构造对象 (仅为阐述目的的成员函数*) | |
使用最外层分配器销毁对象 (仅为阐述目的的成员函数*) |
[edit] 非成员函数
(C++20 中移除) |
比较两个 scoped_allocator_adaptor 对象(函数模板) |
[edit] 推导指引(C++17 起)
[edit] 嵌套类
类 | 定义 |
rebind |
template< class T > struct rebind |
[edit] 示例
#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"); }
[edit] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2108 | C++11 | 没有办法显示 scoped_allocator_adaptor 是否是无状态的 |
提供了 is_always_equal |
[edit] 参见
(C++11) |
提供关于分配器类型的信息 (类模板) |
(C++11) |
检查指定的类型是否支持 uses-allocator 构造 (类模板) |
默认分配器 (类模板) |