
Hi Boosters, I had a very good time writing iterators with the new iterator_facade template. Very good job! It makes writing iterators so much simpler! What about having a similar template for allocators? Given that a large class of useful allocators are de facto built over a couple of typeless malloc-like/free-like functions, I imagine something that could be used like this: struct malloc_impl { static void* allocate(size_t n) { return malloc(n); } static void deallocate(void* p) { free(p); } }; std::vector<int, boost::allocator_facade<int, malloc_impl> > v; where boost::allocator_facade<T, malloc_impl> satisfies all requirements that std::allocator<T> does. An implementation for a conforming compiler and library is straightforward and allocator_facade could also include workarounds for specific platforms where possible (for example it can easily and automatically provide the __stl_alloc_rebind hack in STLport). Is there any interest for such a thing? Alberto

"Alberto Barbati" <abarbati@iaanus.com> wrote in message news:c1baon$fhb$1@sea.gmane.org...
Hi Boosters,
What about having a similar template for allocators? Given that a large class of useful allocators are de facto built over a couple of typeless malloc-like/free-like functions, I imagine something that could be used like this:
struct malloc_impl { static void* allocate(size_t n) { return malloc(n); } static void deallocate(void* p) { free(p); } };
std::vector<int, boost::allocator_facade<int, malloc_impl> > v;
where boost::allocator_facade<T, malloc_impl> satisfies all requirements that std::allocator<T> does.
I think it's worthwhile providing a library template whenever the task of producing a type satisfying standard library requirement can be reduced to satisfying some simpler interface. codecvt's might be another candidate. Personally I don't think I'd use it much, because I rarely write allocators. How general can you make it? What percentage of custom allocators could be defined this way? I'd suggest giving the policy a value_type and making allocator_facade a unary template; if a class of policies are the same except for value_type you can always write a templated policy. Jonathan

Alberto Barbati <abarbati@iaanus.com> writes:
Hi Boosters,
I had a very good time writing iterators with the new iterator_facade template. Very good job! It makes writing iterators so much simpler!
What about having a similar template for allocators? Given that a large class of useful allocators are de facto built over a couple of typeless malloc-like/free-like functions, I imagine something that could be used like this:
struct malloc_impl { static void* allocate(size_t n) { return malloc(n); } static void deallocate(void* p) { free(p); } };
std::vector<int, boost::allocator_facade<int, malloc_impl> > v;
where boost::allocator_facade<T, malloc_impl> satisfies all requirements that std::allocator<T> does.
An implementation for a conforming compiler and library is straightforward and allocator_facade could also include workarounds for specific platforms where possible (for example it can easily and automatically provide the __stl_alloc_rebind hack in STLport).
Is there any interest for such a thing?
My only reservation is that I don't find myself needing to make allocators very often. In fact, the last time I wanted to make something with an allocator interface, I found that the standard interface wasn't appropriate for the project :(. Standard allocators are really only good for containers, AFAICT. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
Alberto Barbati
-
David Abrahams
-
Jonathan Turkanis