beautypg.com

HP Integrity NonStop H-Series User Manual

Page 213

background image

do the actual management of data storage.

To use the allocator interface, a container must meet the following three requirements.

A container needs to have a set of typedefs that look like the following:

typedef Allocator allocator_type;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename Allocator::types::reference reference;
typedef typename Allocator::types::const_reference
const_reference;
typedef implementation_defined iterator;
typedef implementation_defined iterator;

1.

A container also needs to have an Allocator member that will contain a copy the allocator
argument provided by the constructors.

protected:
Allocator the_allocator;

2.

A container needs to use that Allocator member for all storage management. For instance,
our

set

container might have a na_ve implementation that simply allocates a large buffer

and then constructs values on that buffer. Note that this not a very efficient mechanism, but
it serves as a simple example. We're also going to avoid the issue of Allocator::allocate
throwing an exception, in the interest of brevity.

3.

An abbreviated version of the

set

class appears below. The class interface shows the required

typedefs and the Allocator member for this class.

#include

namespace my_namespace {

template
class set
{
public:
// typedefs and allocator member as above
typedef Allocator allocator_type;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type
difference_type;
typedef typename Allocator::types::reference reference;
typedef typename Allocator::types::const_reference
const_reference;

// Our iterator will be a simple pointer

This manual is related to the following products: