The standard containers won't ever call new to allocate memory -
they'll use
allocator supplied with template instantiation and placement new to
invoke
the constructor.
What you probably want to do is to create custom allocator, but not for
use
by new. (I guess your original question was just that).
The API requirements for the allocator are as follows:
(but please, don't quote me on this and better find a refernce
to it in the standard).
template <typename T>
class allocator
{
public:
//Typedefs
typedef T value_type;
typedef value_type * pointer;
typedef value_type const* const_pointer;
typedef value_type& reference;
typedef value_type const& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public:
//The rebind
template <typename U>
struct rebind
{
typedef allocator<U> other;
};
public:
//Constructors/Destructors
allocator() throw() {};
allocator(allocator const&) throw() {};
~allocator() throw() {};
template <typename U>
allocator(allocator<U> const&) throw() {};
//address
pointer address(reference r) const { return &r; };
const_pointer address(const_reference r) const { return &r; };
//memory allocation / deallocation
pointer allocate(size_type cnt, void const * = 0); //Custom
allocation here
void deallocate(pointer p, size_type cnt) throw(); //Custom
deallocation here
//size
size_type max_size() const throw();
//object construction / destrucion
void construct(pointer p, T const& t) const { new (p) T(t); };
void destroy(pointer p) const throw() { p->~T(); };
bool operator == (allocator const&) { return true; };
bool operator != (allocator const& a) { return false; };
};
Gr*****@nospam.com wrote:
Is it safe to do that ? I mean, I'm using custom iterators for legacy
containers already and I need my version of new and delete to be called
by STL algorithms (which already use my custom Iterators).
i.e. I am going to be using stl algorithms with custom containers and
it's essential that our version of new and delete be called. Fair
enough, if yes, just wanted to check.
thanks
G