This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Matching delete operator overload as a template function


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;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]