As far as I know, directly calling the destructor should only be done after having used the placement
new operator to create an object. I only know this since I came across a very special use case for placement new some time ago.
I just had a quick look at the
Array source code, it seems that it indeed uses placement new to place new elements into the preallocated memory block used by the array (which is managed by the
ArrayAllocationBase data member, which uses a simple
HeapBlock for raw memory allocation).
For those who haven’t heard of placement new (like me some month ago):
The usual way of using
new SomeObject would lead to some heap allocation first and then would create the object in the allocated memory location. Calling
delete later on that object (or better let your smart pointer go out of scope ) will lead to first calling the destructor and then freeing the heap memory.
With placement new, you supply a self-managed memory location yourself and just create a new instance of the object there without any previous allocation. This could be stack or heap memory. Now as this memory is managed by the user, calling
delete would be no good idea. Explicitly calling the destructor in this case just leads to a clean destruction routine of the class and then gives you the possibility and responsibility to re-use or free the memory for whatever you want to do with it after that.
So this should also make clear why the code snippet above doesn’t actually reset the float to any value, as @Im_Jimmi said, float as a POD has no destructor.
Array class seems to be one of the most senseful pieces of example code for placement new I came across so far Hope that helps clearing up some peoples questions here and doesn’t sound even more confusing