首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Allocator

封装访问/寻址、分配/解分配以及构建/销毁对象的策略。

每个可能需要分配或释放存储的标准库组件,std::string,,,std::vector,每个容器,除了std::array,到std::shared_ptrstd::function,通过一个Allocator*满足下列要求的类类型的对象。

有些要求是可选的:模板std::allocator_traits为所有可选需求提供默认实现,所有标准库容器和其他分配器感知类通过std::allocator_traits不是直接的。

所需

给予。

  • T,不限定对象类型。
  • A类型的分配器类型。T
  • a,类型的对象A
  • B,某些cv-不限定对象类型的相应分配器类型。U通过重新结合获得的百分之二十八的AsA%29
  • ptr,类型值allocator_traits<A>::pointer,通过调用allocator_traits<A>::allocate()
  • cptr,类型值allocator_traits<A>::const_pointer,通过转换从ptr
  • vptr,类型值allocator_traits<A>::void_pointer,通过转换从ptr
  • cvptr,类型值allocator_traits<A>::const_void_pointer,通过转换从cptr或来自vptr
  • xptr,指向某个cv非限定对象类型的可取消引用的指针。X,,,
  • r,类型的值T由表达式获得*ptr
  • n,类型值allocator_traits<A>::size_type

Expression

Requirements

Return type

A::pointer (optional)

Satisfies NullablePointer, RandomAccessIterator, and ContiguousIterator (see fancy pointers below)

?

A::const_pointer (optional)

A::pointer is convertible to A::const_pointer. Satisfies NullablePointer, RandomAccessIterator, and ContiguousIterator

?

A::void_pointer (optional)

A::pointer is convertible to A::void_pointer B::void_pointer and A::void_pointer are the same type. Satisfies NullablePointer.

?

A::const_void_pointer (optional)

A::pointer, A::const_pointer, and A::void_pointer are convertible to A::const_void_pointer B::const_void_pointer and A::const_void_pointer are the same type. Satisfies NullablePointer.

?

A::value_type

?

the type T

A::size_type (optional)

A::size_type can represent the size of the largest object A can allocate

unsigned integer type

A::difference_type (optional)

A::difference_type can represent the difference of any two pointers to the objects allocated by A

signed integer type

A::template rebind<U>::other (optional1)

for any U, B::template rebind<T>::other is A

the type B

*ptr

?

T&

*cptr

*cptr and *ptr identify the same object

const T&

ptr->m

same as (*ptr).m, if (*ptr).m is well-defined

the type of T::m

cptr->m

same as (*cptr).m, if (*cptr).m is well-defined

the type of T::m

static_cast<A::pointer>(vptr)

static_cast<A::pointer>(vptr) == ptr

A::pointer

static_cast<A::const_pointer>(cvptr)

static_cast<A::const_pointer>(vptr) == cptr

A::const_pointer

std::pointer_traits<A::pointer>::pointer_to(r)

?

A::pointer

a.allocate(n)

allocates storage suitable for n objects of type T, but does not construct them. May throw exceptions.

A::pointer

a.allocate(n, cvptr) (optional)

same as a.allocate(n), but may use cvptr (a pointer obtained from a.allocate() or nullptr_t) in unspecified manner to aid locality

A::pointer

a.deallocate(ptr, n)

deallocates storage pointed to ptr, which must be a value returned by a previous call to allocate that has not been invalidated by an intervening call to deallocate. n must match the value previously passed to allocate. Does not throw exceptions.

(not used)

a.max_size() (optional)

the largest value that can be passed to A::allocate()

A::size_type

a1 == a2

returns true only if the storage allocated by the allocator a1 can be deallocated through a2. Establishes reflexive, symmetric, and transitive relationship. Does not throw exceptions.

bool

a1 != a2

same as !(a1==a2)

bool

A a1(a) A a1 = a.

Copy-constructs a1 such that a1 == a. Does not throw exceptions. (Note: every Allocator also satisfies CopyConstructible)

?

A a(b)

Constructs a such that B(a)==b and A(b)==a. Does not throw exceptions. (Note: this implies that all allocators related by rebind maintain each other's resources, such as memory pools)

?

A a1(std::move(a)) A a1 = std::move(a).

Constructs a1 such that it equals the prior value of a. Does not throw exceptions.

?

A a(std::move(b))

Constructs a such that it equals the prior value of A(b). Does not throw exceptions.

?

a.construct(xptr, args) (optional)

Constructs an object of type X in previously-allocated storage at the address pointed to by xptr, using args as the constructor arguments

?

a.destroy(xptr) (optional)

Destructs an object of type X pointed to by xptr, but does not deallocate any storage.

?

a.select_on_container_copy_construction() (optional)

Provides an instance of A to be used by the container that is copy-constructed from the one that uses a currently. Usually returns either a copy of a or a default-constructed A().

A

A::propagate_on_container_copy_assignment (optional)

true if the allocator of type A needs to be copied when the container that uses it is copy-assigned. Note that if the allocators of the source and the target containers do not compare equal, copy assignment has to deallocate the target's memory using the old allocator and then allocate it using the new allocator before copying the elements (and the allocator).

std::true_type or std::false_type or derived from such

A::propagate_on_container_move_assignment (optional)

true if the allocator of type A needs to be moved when the container that uses it is move-assigned. If this member is false and the allocators of the source and the target containers do not compare equal, move assignment cannot take ownership of the source memory and must move-assign or move-construct the elements individually, resizing its own memory as needed.

std::true_type or std::false_type or derived from such

A::propagate_on_container_swap (optional)

true if the allocators of type A need to be swapped when two containers that use them are swapped. If this member is false and the allocators of the two containers do not compare equal, the behavior of container swap is undefined.

std::true_type or std::false_type or derived from such

A::is_always_equal (since c++17) (optional)

true if any two allocators of type A always compare equal. If not provided, std::allocator_traits defaults this to equal std::is_empty<A>::type

std::true_type or std::false_type or derived from such

注:一重新绑定仅为可选的%28std::allocator_traits%29如果此分配器是窗体的模板SomeAllocator<T, Args>,在哪里Args为零或多个附加模板参数。

Additionally, in order for the type A to satisfy Allocator. If A::propagate_on_container_copy_assignment::value is true, then A must satisfy CopyAssignable and the copy operation must not throw exceptions If A::propagate_on_container_move_assignment::value is true, then A must satisfy MoveAssignable and the move operation must not throw exceptions. If A::propagate_on_container_swap::value is true, lvalues of A must be Swappable and the swap operation must not throw exceptions

(since C++17)

  • 如果A::propagate_on_container_copy_assignment::valuetrue,然后A必须满足CopyAssignable并且复制操作不能抛出异常。
  • 如果A::propagate_on_container_move_assignment::valuetrue,然后A必须满足MoveAssignable移动操作不能抛出异常。
  • 如果A::propagate_on_container_swap::valuetrue的值A一定是Swappable并且交换操作不能抛出异常。

%28自C++17%29

Given. x1 and x2, objects of (possibly different) types X::void_pointer, X::const_void_pointer, X::pointer, or X::const_pointer Then, x1 and x2 are equivalently-valued pointer values, if and only if both x1 and x2 can be explicitly converted to the two corresponding objects px1 and px2 of type X::const_pointer, using a sequence of static_casts using only these four types, and the expression px1 == px2 evaluates to true. Given. w1 and w2, objects of type X::void_pointer. Then for the expression w1 == w2 and w1 != w2 either or both objects may be replaced by an equivalently-valued object of type X::const_void_pointer with no change in semantics. Given. p1 and p2, objects of type X::pointer Then, for the expressions p1 == p2, p1 != p2, p1 < p2 p1 <= p2, p1 >= p2, p1 > p2, p1 - p2} either or both objects may be replaced by an equivalently-valued object of type X::const_pointer with no change in semantics. The above requirements make it possible to compare Container's iterators and const_iterators.

(since C++14)

  • x1x2,对象的%28可能不同的%29类型X::void_pointer,,,X::const_void_pointer,,,X::pointer,或X::const_pointer然后,x1和x2是等价值指针值,当且仅当两者都是x1x2可以显式转换为两个相应的对象。px1px2类型X::const_pointer,使用static_casts只使用这四种类型,以及表达式px1 == px2计算为真。 给予。
  • w1w2,类型对象X::void_pointer...

然后作为表达式w1 == w2w1 != w2可以将其中一个或两个对象替换为等价值类型对象X::const_void_pointer语义不变。

给予。

  • p1p2,类型对象X::pointer然后,对于表达式p1 == p2,,,p1 != p2,,,p1 < p2p1 <= p2,,,p1 >= p2,,,p1 > p2,,,p1 - p2}可以将其中一个或两个对象替换为等价值类型对象X::const_pointer语义不变。 上述要求使比较成为可能。Container%27s迭代器和Const[医]迭代器。 %28自C++14%29分配器完整性要求如果无论T是否为完整类型,分配器类型X都满足分配器完整性要求,则除值外,X是一个完整的类型[医]类型,所有std::分配程序的成员类型。[医]性状<X>都是完整的类型。%28自C++17%29
  • X是一个完整的类型
  • 除了value_type的所有成员类型std::allocator_traits<X>都是完整的类型。

%28自C++17%29

花哨指针

当成员类型pointer不是原始指针类型,它通常被称为“花哨指针”。这些指针是为了支持分段内存体系结构而引入的,现在用于访问与原始指针访问的同构虚拟地址空间不同的地址空间中分配的对象。花哨指针的一个例子是映射地址无关的指针。*进程间::抵消[医]PTR,这样就可以分配基于节点的数据结构,例如std::set在共享内存和内存映射文件中,映射到每个进程的不同地址。可以通过类模板独立于提供这些指针的分配器使用花式指针。std::pointer_traits...

标准库

以下标准库组件满足Allocator概念:

allocator

the default allocator (class template)

scoped_allocator_adaptor (C++11)

implements multi-level allocator for multi-level containers (class template)

polymorphic_allocator (C++17)

an allocator that supports run-time polymorphism based on the std::memory_resource it is constructed with (class template)

实例

一个最小的C++11分配器。

二次

代码语言:javascript
复制
#include <cstddef>
template <class T>
struct SimpleAllocator {
  typedef T value_type;
  SimpleAllocator(/*ctor args*/);
  template <class U> SimpleAllocator(const SimpleAllocator<U>& other);
  T* allocate(std::size_t n);
  void deallocate(T* p, std::size_t n);
};
template <class T, class U>
bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&);
template <class T, class U>
bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&);

二次

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com