class template
<memory>

std::allocator_traits

template <class Alloc> struct allocator_traits;
Allocator traits
This template supplies a uniform interface for allocator types.

The non-specialized version provides an interface that allows to use as allocator just any class that provides at least a public member type value_type and public member functions allocate and deallocate (see example).

But any class type can be used as allocator by specializing this class to give it an interface.

Template 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 typedefinition in unspecialized allocator_traits
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

Static member functions


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// custom allocator example
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>

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 (std::size_t n) { return static_cast<T*>(::operator new(n*sizeof(T))); }
  void deallocate (T* p, std::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 () {
  std::vector<int,custom_allocator<int>> foo = {10,20,30};
  for (auto x: foo) std::cout << x << " ";
  std::cout << '\n';
  return 0;
}

Possible output:
10 20 30