Boost logo

Boost :

From: Douglas Paul Gregor (gregod_at_[hidden])
Date: 2003-03-24 09:02:28


On Mon, 24 Mar 2003, Vladimir Prus wrote:
> Say, I have
>
> std::map<std::string, boost::any> values;
>
> Will I be able to write:
>
> any<fast_allocator> a;
> values[10] = a;
>
> ?
> IOW, I don't think your proposal provides any means to convert between 'any'
> with different allocators. And I'm not sure you can easily achieve that....

Sure you can. You just store a copy of the allocator along with the data
you are holding. The holders would look like this:

struct holder_base {
  virtual ~holder_base() {};
  virtual holder_base* clone() = 0;
  virtual void destroy() = 0;

template<typename T, typename Allocator>
struct holder : holder_base
{
  holder(const T& value, const Allocator& allocator)
    : value(value), allocator(allocator) {}

  ~holder() {}

  virtual holder* clone() = 0
  {
    holder* result = allocator.allocate(1);
    new (result) holder(value, allocator);
    return result;
  }

  virtual void destroy() = 0
  {
    Allocator allocator(this->allocator);
    this->~holder();
    allocator.deallocate(this);
  }
};

This requires allocators to be CopyConstsructible, of course.

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk