An AllocatorAwareContainer is a Container that holds an instance of an Allocator and uses that instance in all its member functions to allocate and deallocate memory and to construct and destroy objects in that memory (such objects may be container elements, nodes, or, for unordered containers, bucket arrays), except that std::basic_string specializations do not use the allocators for construction/destruction of their elements(since C++23).
The following rules apply to container construction:
The only way to replace an allocator is copy-assignment, move-assignment, and swap:
get_allocator()
obtains a copy of the allocator that was used to construct the container or installed by the most recent allocator replacement operation.The only exception is std::basic_string<CharT,Traits,Allocator>::assign, which may also propagate the allocator.
[edit] RequirementsA type satisfies AllocatorAwareContainer if it satisfies Container and, given the following types and values, the semantic and complexity requirements in the tables below are satisfied:
Type DefinitionX
an AllocatorAwareContainer type T
the value_type
of X
A
the allocator type used by X
Value Definition a, b non-const lvalues of type X
c an lvalue of type const X t an lvalue or a const rvalue of type X
rv a non-const rvalue of type X
m a value of type A
[edit] Types Name Type Requirement typename X::allocator_type A
X::allocator_type::value_type
and X::value_type
are the same. [edit] Statements Statement Semantics Complexity X u;
A
is DefaultConstructible. Constant Postcondition u.empty() and u.get_allocator() == A() are both true. X u(m); Postcondition u.empty() and u.get_allocator() == m are both true. Constant X u(t, m); Precondition T
is CopyInsertable into X
. Linear Postcondition u == t and u.get_allocator() == m are both true. X u(rv); Postcondition
T
is MoveInsertable into X
.
A
No direct semantic requirement. Constant a = t X&
Precondition T
is CopyInsertable into X
and CopyAssignable. Linear Postcondition a == t is true. a = rv X&
Precondition If the allocator will not be replaced by move-assignment (see above), then T
is MoveInsertable into X
and MoveAssignable. Linear Effect All existing elements of a are either move assigned to or destroyed. Postcondition If a and rv do not refer the same object, a is equal to the value that rv had before the assignment. a.swap(b) void Effect Exchanges the contents of a and b. Constant [edit] Notes
AllocatorAwareContainers always call std::allocator_traits<A>::construct(m, p, args) to construct an object of type T
at p using args, with m == get_allocator(). The default construct
in std::allocator calls ::new((void*)p) T(args)(until C++20)std::allocator has no construct
member and std::construct_at(p, args) is called when constructing elements(since C++20), but specialized allocators may choose a different definition.
All standard library string types and containers (except std::array and std::inplace_vector) are AllocatorAwareContainers:
stores and manipulates sequences of charactersThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 2839 C++11 self move assignment of standard containers was not allowed allowed but the result is unspecifiedRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4