Przejdź do głównej zawartości

std::allocator_traits

Defined inmemory
Since C++11
template< class Alloc >
struct allocator_traits;

The allocator_traits class template provides the standardized way to access various properties of Allocators. The standard containers and other standard library components access allocators through this template, which makes it possible to use any class type as an allocator, as long as the user-provided specialization of std::allocator_traits implements all required functionality.

A program that declares an explicit or partial specialization of std::allocator_traits is ill-formed, no diagnostic required. (od C++23)

The default, non-specialized, std::allocator_traits contains the following members:

Member types

pubTypeDefinition
puballocator_typeAlloc
pubvalue_typeAlloc::value_type
pubpointerAlloc::pointer if present, otherwise value_type*
pubconst_pointerAlloc::const_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<const value_type>
pubvoid_pointerAlloc::void_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<void>
pubconst_void_pointerAlloc::const_void_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<const_void>
pubdifference_typeAlloc::difference_type if present, otherwise
std::pointer_traits <pointer>::difference_type
pubsize_typeAlloc::size_type if present, otherwise
std::make_unsigned <difference_type>::type
pubpropagate_on_container_copy_assignmentAlloc::propagate_on_container_copy_assignment if present, otherwise
std::false_type
pubpropagate_on_container_move_assignmentAlloc::propagate_on_container_move_assignment if present, otherwise
std::false_type
pubpropagate_on_container_swapAlloc::propagate_on_container_swap if present, otherwise
std::false_type
pubis_always_equalAlloc::is_always_equal if present, otherwise
std::is_empty <Alloc>::type

Member alias templates

pubTypeDefinition
pubrebind_alloc<T>Alloc::rebind<T>::other if present, otherwise Alloc<T, Args> if this Alloc is Alloc<U, Args>
pubrebind_traits<T>std::allocator_traits<rebind_alloc<T>>

Member functions

puballocatestaticallocates uninitialized storage using the allocator
puballocate_at_least(C++23)staticallocates uninitialized storage using the allocator
pubdeallocatestaticdeallocates storage using the allocator
pubconstructstaticconstructs an object in the allocated storage
pubdestroystaticdestructs an object stored in the allocated storage
pubmax_sizestaticreturns the maximum object size supported by the allocator
pubselect_on_container_copy_constructionstaticobtains the allocator to use after copying a standard container

Defect reports

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

DRApplied toBehavior as publishedCorrect behavior
LWG 2108C++11there was no way to show an allocator is statelessis_always_equal provided

std::allocator_traits

Defined inmemory
Since C++11
template< class Alloc >
struct allocator_traits;

The allocator_traits class template provides the standardized way to access various properties of Allocators. The standard containers and other standard library components access allocators through this template, which makes it possible to use any class type as an allocator, as long as the user-provided specialization of std::allocator_traits implements all required functionality.

A program that declares an explicit or partial specialization of std::allocator_traits is ill-formed, no diagnostic required. (od C++23)

The default, non-specialized, std::allocator_traits contains the following members:

Member types

pubTypeDefinition
puballocator_typeAlloc
pubvalue_typeAlloc::value_type
pubpointerAlloc::pointer if present, otherwise value_type*
pubconst_pointerAlloc::const_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<const value_type>
pubvoid_pointerAlloc::void_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<void>
pubconst_void_pointerAlloc::const_void_pointer if present, otherwise
std::pointer_traits <pointer>::rebind<const_void>
pubdifference_typeAlloc::difference_type if present, otherwise
std::pointer_traits <pointer>::difference_type
pubsize_typeAlloc::size_type if present, otherwise
std::make_unsigned <difference_type>::type
pubpropagate_on_container_copy_assignmentAlloc::propagate_on_container_copy_assignment if present, otherwise
std::false_type
pubpropagate_on_container_move_assignmentAlloc::propagate_on_container_move_assignment if present, otherwise
std::false_type
pubpropagate_on_container_swapAlloc::propagate_on_container_swap if present, otherwise
std::false_type
pubis_always_equalAlloc::is_always_equal if present, otherwise
std::is_empty <Alloc>::type

Member alias templates

pubTypeDefinition
pubrebind_alloc<T>Alloc::rebind<T>::other if present, otherwise Alloc<T, Args> if this Alloc is Alloc<U, Args>
pubrebind_traits<T>std::allocator_traits<rebind_alloc<T>>

Member functions

puballocatestaticallocates uninitialized storage using the allocator
puballocate_at_least(C++23)staticallocates uninitialized storage using the allocator
pubdeallocatestaticdeallocates storage using the allocator
pubconstructstaticconstructs an object in the allocated storage
pubdestroystaticdestructs an object stored in the allocated storage
pubmax_sizestaticreturns the maximum object size supported by the allocator
pubselect_on_container_copy_constructionstaticobtains the allocator to use after copying a standard container

Defect reports

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

DRApplied toBehavior as publishedCorrect behavior
LWG 2108C++11there was no way to show an allocator is statelessis_always_equal provided