Search code examples
c++c++17new-operator

Why would a new[] expression ever invoke a destructor?


From the C++17 standard (draft here), [expr.new]:

If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function, and the constructor. If the new-expression creates an array of objects of class type, the destructor is potentially invoked.

Why would new[] invoke a destructor? It's new, after all. It isn't delete.


Solution

  • If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.