C++11 allocator_traits: uses

The C++11 allocator_traits is a class template that provide features to handle the allocator class object (if you don’t know what is allocator class go to this link C++11 allocator). This class will accept allocator class as the template argument type and it defines most of the member functions of the allocator class. Instead of using the member functions of the allocator class we can use the functions provided by this class to perform an operations.

C++11 allocator_traits

The member functions of this type are given below.

i)allocate

ii)deallocate

iii)construct

iv)destroy

v)max_size and

vi)select_on_container_copy_construction.

Below we will see how to use these functions in our program.


allocate( )

The allocate() function callocate memory for the allocatro object. This function accept two arguments. The first argument is the allocator object and the second is the size of the memory to be allocated.

allocator<int> al;

//calling allocator_traits allocate() function
int *iMem=allocator_traits< decltype(alloc) >::allocate( al , 2 ) ;

The allocator_traits allocate() function calls the allocate() function of the allocator class object.The allocator_traits allocate() function will look like this.

//Tp is a type
static Tp* allocate(allocator<Tp>& a, size_t n )
{ return a.allocate( n ); }

Note the function is a static function.


deallocate()

This function will accept three arguments: the first argument is the allocator object, the second is the pointer to the memory and the third is the size of the array element. The purpose of this function is to deallocate the memory.

allocator<int> al;

int *iMem=allocator_traits<decltype(alloc)>::allocate( al ,2 ) ;

//calling allocator_traits deallocate() function
allocator_traits<decltype(al)>::deallocate( al , iMem , 2 ) ;

This function also calls the deallocate() function of the allocator class using the allocator object supplied as the parameter.

//Tp is a type
static void deallocate(allocator<Tp>& a, Tp* pt,size_t n )
{ a.deallocate( pt , n ); }

Note this function also is a static type.


construct()

The purpose of this function is to initialize the memory. The function will accept three arguments: the first argument is the allocator object, the second is the pointer to the memory and the third is the initializer.

allocator<int> al;
int *iMem=allocator_traits<decltype(alloc)>::allocate( al ,2 ) ;

//calling allocator_traits construct() function
allocator_traits<decltype(allocNew)>::construct( al , iMem ,758 ) ;
allocator_traits<decltype(allocNew)>::construct( al , iMem+1 , 546 ) ;

cout<< iMem[0] <<” ” << iMem[1] <<endl ;

allocator_traits<decltype(al)>::deallocate( al , iMem , 2 ) ;

destroy()

The function purpose is to call the destructor of the object. The function accept two arguments: the allocator object and the pointer to the memory.

allocator<int> al;
int *iMem=allocator_traits<decltype(alloc)>::allocate( al ,2 ) ;

allocator_traits<decltype(allocNew)>::construct( al , iMem ,758 ) ;
allocator_traits<decltype(allocNew)>::construct( al , iMem+1 , 546 ) ;

cout<< iMem[0] <<” ” << iMem[1] <<endl ;

//calling destroy() function
allocator_traits<decltype(al)>::destroy( al , iMem ) ;
allocator_traits<decltype(al)>::destroy( al , iMem+1 ) ;

allocator_traits<decltype(al)>::deallocate( al , iMem , 2 ) ;

The destroy() function of allocator_traits calls the destroy() function of the allocator class. The destroy() function will look like this.

//Tp is a type
static void destroy(allocator<Tp>& __a, Tp* pt )
{ __a.destroy( pt ); }


max_size()

The allocator_traits max_size() function like the allocator max_size() function returns the maximum memory size the class can allocate.

allocator<int> al;
cout<< allocator_traits<decltype(al)&gt::max_size(al) ;

select_on_container_copy_construction()

This function will return an allocator object. The allocator object returned is either the object returned by calling the function select_on_container_copy_construction() of the allocator object passed to the function or it simply returns the allocator object that is passed (as argument) to the function. This function will look like this.

//Tp is a type
static allocator<Tp>
select_on_container_copy_construction(const allocator<Tp>& rhs)
{ return rhs.select_on_container_copy_construction(); }

/***
or 
***/

static allocator<Tp>
select_on_container_copy_construction(const allocator<Tp>& rhs)
{ return rhs; }

How to use this function is shown below.

allocator<int> al , alNew ;
int *iMem=al.allocate(2) ;

//calling select_on_container_copy_construction() function
alNew=allocator_traits<decltype(alNew)>::select_on_container_copy_construction( al );

allocator_traits<decltype(alNew)>::construct( allocNew , iMem ,758 ) ;
allocator_traits<decltype(alNew)>::construct( allocNew , iMem+1 , 546 ) ;

allocator_traits<decltype(al)>::deallocate( al , iMem , 2 ) ;

rebinding with allocator_traits class type

In the allocator class there is a rebind member type whose syntax is allocator<type>::rebind<type>::other. This function helps us to create an allocator object of different type from the specified type.Consider the code below.

allocator<int> al ;

decltype(al)::rebind<string>::other stAl ;

We obtain a string type allocator ‘stAl’ from the ‘al’ int type allocator. There exist member type like rebind of allocator in allocator_traits class whose function is exactly the same. How to use this member type is shown below.

allocator<int> al ;

allocator_traits<decltype(al)>::rebind_alloc<string> stAl ;



Leave a Reply

Your email address will not be published. Required fields are marked *