Overloading operator delete problem
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.
While the call to the nothrow-new is easy by specifying
int* p = new(nothrow) int;
how do I call the nothrow-delete
delete (nothrow) p;
does not work.
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) {
^
Thanks for any help finding what I am doing wrong
Ali
#include <new>
#include <cstdlib>
void* operator new(size_t count) throw (std::bad_alloc) {
return malloc(count);
}
void operator delete(void* memory) throw(std::bad_alloc) {
free(memory);
}
void* operator new(size_t count, const std::nothrow_t&) throw () {
return malloc(count);
}
void operator delete(void* memory, const std::nothrow_t&) throw() {
free(memory);
}
int main() {
int* i = new int();
int* j = new(std::nothrow) int();
delete i;
delete /*(std::nothrow)*/ j; // how to specify the nothrow?
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]