Re: is 'operator delete' a function?
kerzum@gmail.com wrote:
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*> v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);
There are several overloaded versions of operator delete, and the cast
selects the one with that signature. But it's probably not the right
solution, because operator delete only releases the memory that the A*
object points to. It doesn't know anything about A's destructor.
What you need is something that will call 'delete ptr' on a pointer
argument ptr. So write a function that does that, or a function object
with an operator() that does that.
struct deleter
{
void operator()(A *ptr) const { delete ptr; }
};
for_each(v.begin(), v.end(), deleter());
--
-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]