I can override the global operator new with different parameters
These are the placement allocation functions.
delete (void *) is the only valid signature.
Not.
First, some terminology: a delete expression, such as delete p
, is not just a function call, it calls the destructor and then calls the deallocation function, which is some kind of operator delete
overload, which is selected using overload resolution.
You can override operator delete
with signatures according to the placement allocation function, but this overload will only be used if the constructor called by the new placement expression throws an exception, for example.
struct E { E() { throw 1; } }; void* operator new(std::size_t n, int) throw(std::bad_alloc) { return new char[n]; } void operator delete(void* p, int) { std::puts("hello!"); delete[] (char*)p; } int main() { try { new (1) E; } catch (...) { puts("caught"); } }
The function of freeing the placement corresponding to the form of the used expression of placement (in this case, it has the int
parameter) is determined by the resolution of the overload and is called to free the storage.
That way you can provide the "delete placement" features, you just can't explicitly name them. You must remember how you selected the object and make sure you use the appropriate release.
source share