This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Matching delete operator overload as a template function
- From: "Jonathan Wakely" <jwakely dot gcc at gmail dot com>
- To: "S. Tang" <stpn9tp02 at sneakemail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Sat, 1 Nov 2008 14:46:26 +0000
- Subject: Re: Matching delete operator overload as a template function
- References: <20351-79600@sneakemail.com>
2008/10/28 S. Tang <stpn9tp02@sneakemail.com>:
> Hello there,
>
> From what I understand, if one uses placement new with a parameter to initialize a class, and if that class' constructor throws an exception, then a matching delete operator is called to release the memory.
>
> This does not seem to work if the overloaded delete operator is a template function.
I think you've found a bug. Here is a reduced test which should fail
to compile, but gcc accepts it:
#include <stddef.h>
template <class T> class undef;
struct MyClass
{
MyClass() { throw 1; } // constructor throws exception
};
template<typename T> class Pool2 { };
template<typename T>
inline void *operator new(size_t size,Pool2<T>& pool)
{
return new char[size];
}
template<typename T>
inline void operator delete(void *p,Pool2<T>& pool)
{
undef<T> t; // ERROR
delete[] (char*)p;
}
int main (int argc, char * const argv[]) {
Pool2<int> pool2;
try {
MyClass *myclass = new (pool2) MyClass(); // delete not called!
} catch(...) { }
return 0;
}