Friday, March 28, 2025

allocator_traits

Overview
This template supplies a uniform interface for allocator types.

Details
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.

Syntax
The syntax is as below. 
template< class Alloc>
struct allocator_traits;

NameDescription
AllocThe allocator type, aliased as member type allocator_type.

Members
The default, non-specialized, std::allocator_traits contains the following members.
member typedefinition
allocator_typeAlloc
value_typeAlloc::value_type
pointerAlloc::pointer if present, otherwise value_type*
const_pointerAlloc::const_pointer if present, otherwise std::pointer_traits<pointer>::rebind<const value_type>
void_pointerAlloc::void_pointer if present, otherwise std::pointer_traits<pointer>::rebind<void>
const_void_pointerAlloc::const_void_pointer if present, otherwise std::pointer_traits<pointer>::rebind<const void>
difference_typeAlloc::difference_type if present, otherwise std::pointer_traits<pointer>::difference_type
size_typeAlloc::size_type if present, otherwise std::make_unsigned<difference_type>::type
propagate_on_container_copy_assignmentAlloc::propagate_on_container_copy_assignment if present, otherwise std::false_type
propagate_on_container_move_assignmentAlloc::propagate_on_container_move_assignment if present, otherwise std::false_type
propagate_on_container_swapAlloc::propagate_on_container_swap if present, otherwise std::false_type
is_always_equalAlloc::is_always_equal if present, otherwise std::is_empty<Alloc>::type

Member alias templates
These are defined on alias template.
member typedefinition
rebind_alloc<T>Alloc::rebind<T>::other if present, otherwise AllocTemplate<T,Args>
value_typeAlloc::value_type

Functionality
Methods
The following static methods are defined.
NameDescription
allocator_type 
allocator_traits::select_on_container_copy_construction
(const allocator_traits::& a)
If possible, obtains the copy-constructed version of the allocator a, by calling a.select_on_container_copy_construction(). If the above is not possible (e.g. Alloc does not have the member function select_on_container_copy_construction()), then returns a, unmodified.
  1. pointer allocator_traits::allocate
    (allocator_type& a, size_type n)
  2. pointer allocator_traits::allocate
    (allocator_type& a, size_type n, const void *  hint)
Uses the allocator a to allocate n * sizeof(value_type) bytes of uninitialized storage. An array of type value_type[n] is created in the storage, but none of its elements are constructed.
  1. Calls a.allocate(n).
  2. Additionally passes memory locality hint hint. Calls a.allocate(n, hint) if possible. If not possible (e.g. a has no two-argument member function allocate), calls a.allocate(n).
void allocator_traits::deallocate
(allocator_type& a, pointer p, size_type n)
Uses the allocator a to deallocate the storage referenced by p, by calling a.deallocate(p, n).
size_type allocator_traits::max_size
(allocator_type& a)
If possible, obtains the maximum theoretically possible allocation size from the allocator a, by calling a.max_size().
If the above is not possible (e.g., a does not have the member function max_size()), then returns std::numeric_limits<size_type>::max() / sizeof(value_type).
template< class U, class... Args >
void allocator_traits::construct
(allocator_type& a, U* p, Args&&... args )
Using allocator a, constructs an object of type T in allocated uninitialized storage pointed to by p, using
a.construct(p, std::forward<Args>(args)...)If it's not possible, global placement-new is used.
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...).
template< class >
void allocator_traits::destroy
(allocator_type& a, U* p )
Calls the destructor of the object pointed to by p. If possible, does so by calling a.destroy(p). If not possible (e.g. a does not have the member function destroy()), then calls the destructor of *p directly, as p->~U().

The following Example 14 demonstrates usage of specialized allocator class that uses windows shared memory.


No comments:

Post a Comment