Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2005-01-08 18:00:25


"Jason Hise" wrote:

> I have recently converted my code to CRTP form and noticed an inherent
> problem with creating an allocator template now. Because the derived
> class is the final singleton class, and its ctor and dtor are protected,
> there is no way to give a generic allocator the right to create the
> object.
Allocator just allocates N bytes, object creates itself via
placement new.

   static T * Create ( )
   {
       void * p = Allocator::allocate(sizeof(T));
       try
       {
           return new (p) T(...);
       }
       catch ( ... )
        {
            Allocator::deallocate(p);
            throw;
        }
    }

Instead of try/catch shared_array should be used to avoid
destroying stack frame when exception throws.

/Pavel


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