Boost logo

Boost Users :

From: Mark A. Gibbs (gibbsmark_at_[hidden])
Date: 2003-11-16 12:54:16


I have a couple of questions about combining smart pointers and STL
allocators.

First of all, am I right in assuming that I can use the shared_*
templates right out of the box, as long as I define a custom deleter
that uses the allocator's deallocate function?

But if that's so, how exactly is it done? Part of my confusion comes
from the fact that the STL allocator documentation I've come across
isn't exactly clear on the semantics of allocators (I don't have a copy
of the standard). For instance, I'm not 100% sure whether or not I have
to call construct() after allocate() and destroy() before deallocate().
This is what I have so far:

class allocator_helper
{
public:
    allocator_helper(allocator_type& a) : a_(a)
    {}

    void operator()(void const* p) const
    {
       a_.deallocate(static_cast<allocator_type::pointer>(p));
    }

private:
    allocator_type& a_;
};

foo* bar = static_cast<foo*>(get_allocator().allocate(1));
shared_ptr<foo> p_foo(bar, allocator_helper(get_allocator()));

foo* baz = static_cast<foo*>(get_allocator().allocate(10));
shared_array<foo> p_foo(baz, allocator_helper(get_allocator()));

Now, for scoped_* I would probably have to rewrite them to use the
allocator, right? Or is there a way to use allocators with the existing
scoped_* templates?

Mark


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net