Either we do it this way or we share the allocator across the container
and
smart pointer so that the smart pointer uses deallocate(void *):
1)
std::vector<shifted_ptr<int>, shifted_allocator<shifted_ptr<int> > > c1;
2)
std::vector<shifted_ptr<int, shifted_allocator<int> >,
shifted_allocator<shifted_ptr<int, shifted_allocator<int> > > > c2;
I don't even think option 2) is implementable.
I may just be dense, but I still don't see what you're getting at.
Can't you just use the deleter parameter to indicate special deletion?
Something like this (untested):
class C { /*...*/ };
template<class Alloc>
class MyDeleter
{
Alloc& m_alloc;
typedef typename Alloc::value_type T;
public:
MyDeleter( Alloc& alloc ) : m_alloc( alloc ) {}
// Might want a const T* version also
void operator()( T* const ptr )
{
m_alloc.destroy( ptr );
m_alloc.deallocate( ptr, sizeof(T) );
}
};
void Foo( MyAllocator<C>& alloc, const C& initVal )
{
typedef std::vector< std::tr1::shared_ptr<C> > VSPC;
VSPC v( 10 );
for( VSPC::iterator it=v.begin(); it != v.end(); ++it )
{
// Could hide these next lines in a factory function
C* const c = alloc.allocate( sizeof(C) );
alloc.construct( c, initVal );
it->reset( c, MyDeleter( m_alloc ) );
}
// ...
}
Now, when the vector "v" goes out of scope, the shared_ptrs use the
"alloc" object to deallocate their pointees.
explaining. In my case I was using the allocator passed in as a template
overloaded constructor shared_ptr(T *, D *). I can't imagine calling this
way. If I forget calling it for one pointer the compiler will not report
any compilation error.