allocator with no template

Posted by Merni on Stack Overflow See other posts from Stack Overflow or by Merni
Published on 2010-05-06T17:38:05Z Indexed on 2010/05/06 17:48 UTC
Read the original article Hit count: 186

Filed under:
|
|

Every stl container take an allocator as a second object,

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

If you write your own class It is possible to use your own allocator. But is it possible to write your own allocator without using templates?

For example, writing this function is not easy if you are not allowed to use templates

 pointer allocate(size_type n, const_pointer = 0) {
    void* p = std::malloc(n * sizeof(T));
    if (!p)
      throw std::bad_alloc();
    return static_cast<pointer>(p);
  }

Because how could you know the size of T?

© Stack Overflow or respective owner

Related posts about memory-allocation

Related posts about c++