--- "E. Gladyshev" <[EMAIL PROTECTED]> wrote:
> 
> --- Peter Dimov <[EMAIL PROTECTED]> wrote:
> > E. Gladyshev wrote:
> > > --- Peter Dimov <[EMAIL PROTECTED]> wrote:
> > >
> > >> unless there are very solid reasons for the allocator parameter. ;-)
> > >
> > > I agree, but the problme is that I don't know whether I have a solid
> > reason or not.
> > > I have to ship my class to someone else. She might have a solid reason, I
> > don't know.
> > > If I supply an allocator parameter, then I am covered and I can sleep
> > well. :)
> > 
> > Well... if you ask me, it is better to not offer the functionality until
> > someone explicitly asks for it and gives a reasonable scenario as an
> > example. Features are easy to add, hard to remove, and there is always the
> > possibility that you "pre-included" the wrong feature.
> 
> Peter, using your example I put together an allocator class that can be used in 
> shared_ptr and
> STL
> at the same time.  It is not too pretty but it works.  If we could only customize 
> the counter
> allocations, that would be it.
> 
> namespace boostx
> {
>       template< typename A >
>       class allocator : public A
>       {
>       public:
>               typedef A::value_type type;
>               allocator() {}
> 
>               type* create()
>               {
>                       type* p = allocate(1, 0);
>                       try
>                       {
>                               p = new(p) type;
>                       }
>                       catch(...)
>                       {
>                               deallocate( p, 1 );
>                               throw;
>                       }
>                       return p;
>               }
> 
>               void operator()( type* p )
>               {
>                       try
>                       {
>                               p->~T();
>                       }
>                       catch(...)
>                       {
>                               deallocate(p, 1);
>                               throw;
>                       }
>                       deallocate(p, 1);
>               }
>       };
> };
> 
> template < class T, class A = boostx::allocator< std::allocator<T> > >
> struct X
> {
>       boost::shared_ptr<T> _data;
>       std::vector<T,A> _list;
> 
>       X( const A& al = A() ) : _list(al)
>       { 
>               A tmp(al);
>               _data = boost::shared_ptr<T>( tmp.create(), tmp );
>       }
> };
> 
> OR if you don't mind dropping the 'const' from the constructor
> 
> template < typename T, class A = boostx::allocator< std::allocator<T> > >
> struct X
> {
>       boost::shared_ptr<T> _data;
>       std::vector<T,A> _list;
> 
>       X( A& al = A() ) : _list(al), _data( al.create(), al )
>       { 
>       }
> };
> 
> 
> Eugene

I forgot to mention, if the 'const' is dropped from the constructor, 'A' must be 
stateless.

Eugene


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to