I am just learning about C++ allocators, and I am trying to understand the purpose of the struct rebind
in every allocator. For example, in this program:
#include <memory>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int>::allocator_type IntAlloc;
int main( )
{
IntAlloc v1Iter;
vector<int> v1;
//************What's going on here?********
IntAlloc::rebind<char>::other::pointer pszC =
IntAlloc::rebind<char>::other(v1.get_allocator()).allocate(1, (void *)0);
int * pInt = v1Iter.allocate(10);
}
I am trying to understand what the key line is doing. Is it modifying the typedef
IntAlloc
to now server as an allocator for a char
vector
? Even if I have guessed correctly, I am not sure I can break it down. Here's my guess:
IntAlloc::rebind<char> //accessing the struct rebind
`::other` //accessing data member other inside rebind
(v1.get_allocator()) //**isn't this just retrieving IntAlloc in its original form?**
///What is this for?
.allocate(1, (void *)0); //**this is allocating something? What do these parameters mean?**