在线文档教程
C++
动态内存管理 | Dynamic memory management

std::scoped_allocator_adaptor

STD:范围[医]分配器[医]适配器

Defined in header
template< class OuterAlloc, class... InnerAlloc > class scoped_allocator_adaptor : public OuterAlloc;(since C++11)

std::scoped_allocator_adaptor类模板是一个分配器,它可以与多级容器%28向量、映射元组列表的集合等一起使用。它是用一个外部分配器类型实例化的。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...>作为成员对象。

成员类型

TypeDefinition
outer_allocator_typeOuterAlloc
inner_allocator_typescoped_allocator_adaptor<InnerAllocs...> or, if sizeof...(InnerAllocs) == 0, scoped_allocator_adaptor<OuterAlloc>
value_typestd::allocator_traits<OuterAlloc>::value_type
size_typestd::allocator_traits<OuterAlloc>::size_type
difference_typestd::allocator_traits<OuterAlloc>::difference_type
pointerstd::allocator_traits<OuterAlloc>::pointer
const_pointerstd::allocator_traits<OuterAlloc>::const_pointer
void_pointerstd::allocator_traits<OuterAlloc>::void_pointer
const_void_pointerstd::allocator_traits<OuterAlloc>::const_void_pointer

传播[医]上[医]集装箱[医]复制[医]任务STD::真[医]输入if std::分配器[医]性状<A>*宣传[医]上[医]集装箱[医]复制[医]

传播[医]上[医]集装箱[医]移动[医]任务STD::真[医]输入if std::分配器[医]性状<A>*宣传[医]上[医]集装箱[医]移动[医]任务:在Outeralloc和Inneralloc之间至少有一个分配器A的值为真。

传播[医]上[医]集装箱[医]SWAP STD::true[医]输入if std::分配器[医]性状<A>*宣传[医]上[医]集装箱[医]交换:值是对至少一个分配器A在Outeralloc和Inneralloc之间。

是[医]总[医]等于%28C++17%29 std::true[医]输入if std::分配器[医]性状<A>*IS[医]总[医]===

重新绑定模板<class T>结构重新绑定{ty胡枝子f作用域分配器[医]适配器<std::分配程序[医]性状<OuterAlloc>*模板重新绑定[医]异种<T>,InnerAllocs...>Other;};

成员函数

(constructor)creates a new scoped_allocator_adaptor instance (public member function)
(destructor)destructs a scoped_allocator_adaptor instance (public member function)
operator=assigns a scoped_allocator_adaptor (public member function)
inner_allocatorobtains an inner_allocator reference (public member function)
outer_allocatorobtains an outer_allocator reference (public member function)
allocateallocates uninitialized storage using the outer allocator (public member function)
deallocatedeallocates storage using the outer allocator (public member function)
max_sizereturns the largest allocation size supported by the outer allocator (public member function)
constructconstructs an object in allocated storage, passing the inner allocator to its constructor if appropriate (public member function)
destroydestructs an object in allocated storage (public member function)
select_on_container_copy_constructioncopies the state of scoped_allocator_adaptor and all its allocators (public member function)

非会员职能

operator==operator!=compares two scoped_allocator_adaptor instances (public member function)

二次

#include <vector> #include <scoped_allocator> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/adaptive_pool.hpp> 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 alloc<int> a(s.get_segment_manager() // create vector of vectors in shared memory ipc_matrix v(a // note: rebinds alloc<ipc_row> from this alloc<int> // 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" }

二次

另见

allocator_traits (C++11)provides information about allocator types (class template)
uses_allocator (C++11)checks if the specified type supports uses-allocator construction (class template)
allocatorthe default allocator (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Memory/作用域[医]分配器[医]适配器