|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AnAllocatorAwareContainer is aContainer that holds an instance of anAllocator 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 thatstd::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 isstd::basic_string<CharT,Traits,Allocator>::assign, which may also propagate the allocator.
Contents |
A type satisfiesAllocatorAwareContainer if it satisfiesContainer and, given the following types and values, the semantic and complexity requirements in the tables below are satisfied:
| Type | Definition |
X | anAllocatorAwareContainer type |
T | thevalue_type ofX |
A | the allocator type used byX |
| Value | Definition |
| a,b | non-const lvalues of typeX |
| c | an lvalue of typeconst X |
| t | an lvalue or a const rvalue of typeX |
| rv | a non-const rvalue of typeX |
| m | a value of typeA |
| Name | Type | Requirement |
|---|---|---|
| typename X::allocator_type | A | X::allocator_type::value_type andX::value_type are the same. |
| Statement | Semantics | Complexity | |
|---|---|---|---|
| X u; X u= X(); | Precondition | A isDefaultConstructible. | Constant |
| Postcondition | u.empty() andu.get_allocator()== A() are bothtrue. | ||
| X u(m); | Postcondition | u.empty() andu.get_allocator()== m are bothtrue. | Constant |
| X u(t, m); | Precondition | T isCopyInsertable intoX. | Linear |
| Postcondition | u== t andu.get_allocator()== m are bothtrue. | ||
| X u(rv); | Postcondition |
| Constant |
| X u(rv, m); | Precondition | T isMoveInsertable intoX. |
|
| Postcondition |
| ||
| Expression | Type | Semantics | Complexity | |
|---|---|---|---|---|
| c.get_allocator() | A | No direct semantic requirement. | Constant | |
| a= t | X& | Precondition | T isCopyInsertable intoX andCopyAssignable. | Linear |
| Postcondition | a== t istrue. | |||
| a= rv | X& | Precondition | If the allocator willnot be replaced by move-assignment (seeabove), thenT isMoveInsertable intoX andMoveAssignable. | Linear |
| Effect | All existing elements ofa are either move assigned to or destroyed. | |||
| Postcondition | Ifa andrv do not refer the same object,a is equal to the value thatrv had before the assignment. | |||
| a.swap(b) | void | Effect | Exchanges the contents ofa andb. | Constant |
AllocatorAwareContainers always callstd::allocator_traits<A>::construct(m, p, args) to construct an object of typeT atp usingargs, withm== get_allocator().The defaultconstruct instd::allocator calls::new((void*)p) T(args)(until C++20)std::allocator has noconstruct member andstd::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 (exceptstd::array andstd::inplace_vector) areAllocatorAwareContainers:
| stores and manipulates sequences of characters (class template)[edit] | |
| double-ended queue (class template)[edit] | |
(C++11) | singly-linked list (class template)[edit] |
| doubly-linked list (class template)[edit] | |
| resizable contiguous array (class template)[edit] | |
| collection of key-value pairs, sorted by keys, keys are unique (class template)[edit] | |
| collection of key-value pairs, sorted by keys (class template)[edit] | |
| collection of unique keys, sorted by keys (class template)[edit] | |
| collection of keys, sorted by keys (class template)[edit] | |
(C++11) | collection of key-value pairs, hashed by keys, keys are unique (class template)[edit] |
(C++11) | collection of key-value pairs, hashed by keys (class template)[edit] |
(C++11) | collection of unique keys, hashed by keys (class template)[edit] |
(C++11) | collection of keys, hashed by keys (class template)[edit] |
The 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 unspecified |