1

A vector is defined as

template < class T, class Alloc = allocator<T> > class vector;

Every vector constructor (or one overload of each type) has an allocator overload and the default constructor has one as well. An allocator is specified already in the class template. What is the constructor allocator for?

From http://www.cplusplus.com/reference/vector/vector/vector/

default (1) 

explicit vector (const allocator_type& alloc = allocator_type());

fill (2)    

explicit vector (size_type n);
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());

range (3)   

template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());

copy (4)    

vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);

move (5)    

vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);

initializer list (6)    

vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
user2814152
  • 197
  • 9

2 Answers2

2

The argument in the template brackets is the type (the class) of the allocator. The argument to the constructor is an instance of that type.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • oh! I see it now, it uses class up there and not a specific instance. That's kind of strange when they could provide the instance in the template – user2814152 Sep 25 '13 at 07:49
  • @user2814152: No they couldn't. The language does not allow arbitrary objects to be passed as template arguments. – Benjamin Lindley Sep 25 '13 at 07:50
  • I don't mean instance. Something like int here http://stackoverflow.com/questions/3082113/calculating-factorial-using-template-meta-programming – user2814152 Sep 25 '13 at 07:54
  • @user2814152: I knew what you meant. Only types and integer constants can be passed as template parameters, not objects of arbitrary types. – Benjamin Lindley Sep 25 '13 at 07:56
  • After writing I believe I expected it to be [written like this](http://ideone.com/Vo1w72). Now i'm trying to understand why it isn't and googling allocator tutorials – user2814152 Sep 25 '13 at 08:38
  • nevermind I get it now after seeing http://www.josuttis.com/libbook/memory/myalloc1.cpp.html it has instance state. But it's hard for me to understand why the implementation i'm looking at inherit from allocator and why would it be useful to pass have the allocator const in the constructor. – user2814152 Sep 25 '13 at 08:58
0

According to this interview of Alex Stepanov that appeared in the March 1995 issue of Dr. Dobb's Journal, allocators are a result of needing to accommodate different memory models. Addressing x86 memory with 16 bit code was rather weird and there was something like 5 different memory models you could use to do it.

People wanted containers independent of the memory model, which was somewhat excessive because the language doesn't include memory models. People wanted the library to provide some mechanism for abstracting memory models. Earlier versions of STL assumed that the size of the container is expressible as an integer of type size_t and that the distance between two iterators is of type ptrdiff_t. And now we were told, why don't you abstract from that? It's a tall order because the language does not abstract from that; C and C++ arrays are not parameterized by these types. We invented a mechanism called "allocator," which encapsulates information about the memory model. That caused grave consequences for every component in the library. You might wonder what memory models have to do with algorithms or the container interfaces. If you cannot use things like size_t, you also cannot use things like T* because of different pointer types (T*, T huge *, etc.). Then you cannot use references because with different memory models you have different reference types. There were tremendous ramifications on the library. - Alex Stepanov

James Hollis
  • 176
  • 6