There are mainly two cases:
The first is when -for example in an embedded system- you have to construct an object in a given well-known place.
The second is when you want -for some reason- to manage memory in a way other than the default.
In C++, an expression like
pA = new(...) A(...) does two consecutive things:
- calls the
void* operator new(size_t, ...) function and subsequently
Since calling new is the only way to call A::A(), adding parameters to new allows to specialize different way to manage memory. The most trivial is to "use memory already obtained by some other means".
This method is fine when the allocation and construction needs to be separated. The typical case is
std::allocator, whose purpose is allocate uninitialized memory for a given quantity, while object contruction happens later.
This happens, for example, in
std::vector, since it has to allocate a
capacity normally wider than its actual
size, and then contruct the object as they are
push_back-ed in the space that already exist.
In fact the default std::allocator implementation, when asket to allocate n object, does a
return reinterpret_cast<T*>(new char[n*sizeof(T)]), so allocating the space, but actually not constructing anything.
Admitting that std::vector stores:
T* pT; //the actual buffer
size_t sz; //the actual size
size_t cap; //the actual capacity
an implemetation of push_back can be:
void vector<T>::push_back(const T& t)
size_t ncap = cap + 1+ cap/2; //just something more than cap
T* npT = alloc.allocate(ncap);
for(size_t i=0; i<sz; ++i)
new(npT+i)T(pt[i]); //copy old values (may be move in C++11)
pt[i].~T(); // destroy old value, without deallocating
pT = npT;
cap = ncap;
// now we heve extra capacity
new(pT+sz)T(t); //copy the new value
++sz; //actual size grown
In essence, there is the need to separate the allocation (that relates to the buffer as a whole) with the construction of the elements (that has to happen in the already existent bufffer).