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 types | Definition |
---|---|
allocator_type | the template parameter (Alloc) |
value_type | allocator_type::value_type |
pointer | allocator_type::pointer (if defined) value_type* (otherwise) |
const_pointer | allocator_type::const_pointer (if defined) pointer_traits<pointer>::rebind<const value_type> (otherwise) |
void_pointer | allocator_type::void_pointer (if defined) pointer_traits<pointer>::rebind<void> (otherwise) |
const_void_pointer | allocator_type::const_void_pointer (if defined) pointer_traits<pointer>::rebind<const void> (otherwise) |
difference_type | allocator_type::difference_type (if defined) pointer_traits<pointer>::difference_type (otherwise) |
size_type | allocator_type::size_type (if defined) make_unsigned<difference_type>::type (otherwise) |
propagate_on_container_copy_assignment | allocator_type::propagate_on_container_copy_assignment (if defined) false_type (otherwise) |
propagate_on_container_move_assignment | allocator_type::propagate_on_container_move_assignment (if defined) false_type (otherwise) |
propagate_on_container_swap | allocator_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
Functions | Description |
---|---|
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