Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

15.3 Building Your Own Allocators

Defining your own allocator is a relatively simple process. The C++ Standard Library describes a particular interface, consisting of types and functions. An allocator that conforms to the standard must match the syntactic requirements for these member functions and types. The C++ Standard Library also specifies a portion of the semantics for the allocator type.

15.3.1 Using the Standard Allocator Interface

An allocator that conforms to the C++ Standard Library allocator specification must have the following interface. The example uses my_allocator as a place holder for your own allocator name:

The rebind member allows a container to construct an allocator for some arbitrary type out of the allocator type provided as a template parameter. For instance, the list container gets an allocator<T> by default, but a list may well need to allocate list_nodes as well as T's. The container can construct an allocator for list_nodes out of the allocator for T, which is the template parameter Allocator in this case, as follows:

Here is a description of the member functions that an allocator class template must provide:

template <class U>
my_allocator(const my_allocator<U>&);
template <class U>
operator=(const my_allocator<U>&);
pointer address(reference r) const;
const_pointer address(const_reference r) const;
pointer allocate(size_type n, allocator<U>::const_pointer hint=0);
construct(pointer p, const_reference val);
destroy(pointer p);

Additionally, user-defined allocators must be equality comparable: if a and b are instances of a user-defined allocator, then the expressions (a == b) and (a != b) must be well-formed.

Previous fileTop of DocumentContentsIndex pageNext file