Przejdź do głównej zawartości

C++ named requirements: AllocatorAwareContainer (od C++11)

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 (od C++23).

The following rules apply to container construction

  • Copy constructors of AllocatorAwareContainers obtain their instances of the allocator by calling std::allocator_traits<allocator_type>::select_on_container_copy_construction on the allocator of the container being copied.
  • Move constructors obtain their instances of allocators by move-constructing from the allocator belonging to the old container.
  • All other constructors take a const allocator_type& parameter.

The only way to replace an allocator is copy-assignment, move-assignment, and swap:

  • Copy-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value is true
  • Move-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value is true
  • Swap will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_swap::value is true
    Specifically, it will exchange the allocator instances through an unqualified call to the non-member function swap, see Swappable.

Note: swapping two containers with unequal allocators if propagate_on_container_swap is false is undefined behavior.

  • The accessor get_allocator() obtains a copy of the allocator that was used to construct the container or installed by the most recent allocator replacement operation.

Requirements

Legend

X - Container type
T - Element type
A - Allocator for T
a, b - Objects of type X (non-const lvalue)
t - Object of type X (lvalue or const rvalue)
rv - Object of type X (non-const rvalue)
m - Object of type A

ExpressionReturn typePre/RequirementsPost/EffectsComplexity
allocator_typeAallocator_type::value_type must be the same as X::value_typecompile-time
get_allocator()Aconstant
X u;A is DefaultConstructibleu.empty() == true && u.get_allocator() == A()constant
X u(m);u.empty() == true && u.get_allocator() == mconstant
X u(t,m);T is CopyInsertable into Xu == t && u.get_allocator() == mlinear
X u(rv);Move constructor of A must not throw exceptionsu has the same elements and an equal allocator as rv had before the constructionconstant
X u(rv,m);T is MoveInsertable into XThe elements of u are the same or copies of those of rv and u.get_allocator() == mconstant if m == rv.get_allocator(), otherwise linear
a = tX&T is CopyInsertable into X and CopyAssignablea == tlinear
a = rvX&If the allocator will not be replaced by move-assignment (see above), then T is MoveInsertable into X and MoveAssignableAll existing elements of a are either move assigned to or destroyed; if a and rv do not refer the same object, a is equal to the value that rv had before the assignmentlinear
a.swap(b)voidExchanges the contents of a and bconstant

Notes

Allocator-aware containers 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)  (do C++20) std::allocator has no construct member and std::construct_at(p, args) is called when constructing elements  (od C++20), but specialized allocators may choose a different definition

Standard library

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2839C++11self move assignment of standard containers was not allowedallowed but the result is unspecifed

C++ named requirements: AllocatorAwareContainer (od C++11)

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 (od C++23).

The following rules apply to container construction

  • Copy constructors of AllocatorAwareContainers obtain their instances of the allocator by calling std::allocator_traits<allocator_type>::select_on_container_copy_construction on the allocator of the container being copied.
  • Move constructors obtain their instances of allocators by move-constructing from the allocator belonging to the old container.
  • All other constructors take a const allocator_type& parameter.

The only way to replace an allocator is copy-assignment, move-assignment, and swap:

  • Copy-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value is true
  • Move-assignment will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value is true
  • Swap will replace the allocator only if std::allocator_traits<allocator_type>::propagate_on_container_swap::value is true
    Specifically, it will exchange the allocator instances through an unqualified call to the non-member function swap, see Swappable.

Note: swapping two containers with unequal allocators if propagate_on_container_swap is false is undefined behavior.

  • The accessor get_allocator() obtains a copy of the allocator that was used to construct the container or installed by the most recent allocator replacement operation.

Requirements

Legend

X - Container type
T - Element type
A - Allocator for T
a, b - Objects of type X (non-const lvalue)
t - Object of type X (lvalue or const rvalue)
rv - Object of type X (non-const rvalue)
m - Object of type A

ExpressionReturn typePre/RequirementsPost/EffectsComplexity
allocator_typeAallocator_type::value_type must be the same as X::value_typecompile-time
get_allocator()Aconstant
X u;A is DefaultConstructibleu.empty() == true && u.get_allocator() == A()constant
X u(m);u.empty() == true && u.get_allocator() == mconstant
X u(t,m);T is CopyInsertable into Xu == t && u.get_allocator() == mlinear
X u(rv);Move constructor of A must not throw exceptionsu has the same elements and an equal allocator as rv had before the constructionconstant
X u(rv,m);T is MoveInsertable into XThe elements of u are the same or copies of those of rv and u.get_allocator() == mconstant if m == rv.get_allocator(), otherwise linear
a = tX&T is CopyInsertable into X and CopyAssignablea == tlinear
a = rvX&If the allocator will not be replaced by move-assignment (see above), then T is MoveInsertable into X and MoveAssignableAll existing elements of a are either move assigned to or destroyed; if a and rv do not refer the same object, a is equal to the value that rv had before the assignmentlinear
a.swap(b)voidExchanges the contents of a and bconstant

Notes

Allocator-aware containers 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)  (do C++20) std::allocator has no construct member and std::construct_at(p, args) is called when constructing elements  (od C++20), but specialized allocators may choose a different definition

Standard library

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2839C++11self move assignment of standard containers was not allowedallowed but the result is unspecifed