When you are implementing a vector
, you probably want to keep the management of the storage and the management of the objects separate.
Storage is managed using allocator::allocate(size_type n)
and allocator::deallocate(pointer p, size_type n)
, which are typically implemented as return ::operator new(n * sizeof (T));
and ::operator delete(p);
respectively.
These functions allocate and deallocate memory, but do not call constructors or destructors for objects contained in that memory.
Object lifetimes are managed using allocator::construct(U* p, Args&&... args);
and allocator::destroy(U* p);
, which are typically implemented as ::new((void *)p) U(std::forward<Args>(args)...);
and p->~U()
respectively.
These functions construct and deconstruct objects in preallocated memory, but do not themselves manage the memory.