Re: Overloading operator delete problem
albrecht.fritzsche wrote:
Hi,
I've overloaded operator new and operator delete like in the appended
code but don't know how to trigger a call to the nothrow-delete.
....
At least on Vis C++ the appended code compiles but uses for the last
delete the throwing-delete version. How do I have to specify the
std::nothrow? Where is this actually documented in the standard?
And, why do I get a strange error from Comeau (online) with exactly
this code saying
"ComeauTest.c", line 6: error: exception specification is incompatible with that of
previous function "operator delete(void *)" (declared at line 67 of
"new.stdh"):
previously specified: no exceptions will be thrown
void operator delete(void* memory) throw(std::bad_alloc) {
#include <new>
#include <cstdlib>
....
void operator delete(void* memory) throw(std::bad_alloc) {
free(memory);
}
This declaration of delete() is incorrect. After all, how could delete
have a problem allocating memory when all that the function does is to
free memory? Furthermore, since throwing exceptions while destructing
objects is usually fatal to a program, it turns out that neither the
nothrow or non-nothrow delete() throws exceptions:
void operator delete(void* memory) throw() {
free(memory);
}
Of course one may well wonder why a nothrow version of delete() exists
at all - since its behavior seems to be indistinguishable from the
non-nothrow version.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]