C++ Standard Library C++ STL Library

C++ <memory> - allocator_traits class template



The C++ <memory> 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 allocator_traits implements all required functionality.

Syntax

template <class Alloc> struct allocator_traits;

Parameters

Alloc The allocator type, aliased as member type allocator_type.

Member Types

The following aliases are member types of allocator_traits. Any specialization shall also define these member types:

Member typesDefinition
allocator_typethe template parameter (Alloc)
value_typeallocator_type::value_type
pointerallocator_type::pointer (if defined)
value_type* (otherwise)
const_pointerallocator_type::const_pointer (if defined)
pointer_traits<pointer>::rebind<const value_type> (otherwise)
void_pointerallocator_type::void_pointer (if defined)
pointer_traits<pointer>::rebind<void> (otherwise)
const_void_pointerallocator_type::const_void_pointer (if defined)
pointer_traits<pointer>::rebind<const void> (otherwise)
difference_typeallocator_type::difference_type (if defined)
pointer_traits<pointer>::difference_type (otherwise)
size_typeallocator_type::size_type (if defined)
make_unsigned<difference_type>::type (otherwise)
propagate_on_container_copy_assignmentallocator_type::propagate_on_container_copy_assignment (if defined)
false_type (otherwise)
propagate_on_container_move_assignmentallocator_type::propagate_on_container_move_assignment (if defined)
false_type (otherwise)
propagate_on_container_swapallocator_type::propagate_on_container_swap (if defined)
false_type (otherwise)
rebind_alloc<T>allocator_type::rebind<T>::other (if defined)
AllocTemplate<T,Args> (if Alloc is an instantiation of the form
AllocTemplate<U,Args>, where Args are zero or more type arguments)
Note: This is an alias template
rebind_traits<T>allocator_traits<rebind_alloc<T>>
Note: This is an alias template

Member Functions

FunctionsDescription
allocate Allocate block of storage
deallocate Release block of storage
construct Construct an element
destroy Destroy object
max_size Maximum size possible to allocate
select_on_container_copy_construction Select on container copy construction

Example:

The example below shows how to use custom allocator with a standard library container.

#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>

using namespace std;

template <class T>
struct custom_allocator {
  typedef T value_type;
  custom_allocator() noexcept {}
  template <class U> custom_allocator (const custom_allocator<U>&) noexcept {}
  T* allocate (size_t n) {return static_cast<T*>(::operator new(n*sizeof(T))); }
  void deallocate (T* p, size_t n) {::delete(p); }
};

template <class T, class U>
constexpr bool operator== (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return true;}

template <class T, class U>
constexpr bool operator!= (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return false;}

int main () {
  vector<int,custom_allocator<int>> foo = {100, 200, 300};
  for (auto x: foo) cout<<x<<" ";
  cout<<endl;
  return 0;
}

The output of the above code will be:

100 200 300 

❮ C++ <memory> Library