is 'operator delete' a function?

N

Nindi

On comp.lang.c++.moderated

http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::eek:pearator 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*) ::eek:perator delete);

-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .


#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;


};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T> & theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<T> theInnerPtr;
};


int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base> > theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";


}



I am not subscribed there yet .. so couldn't post there.
 
S

Shelyag Yuriy

Nindi said:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::eek:pearator 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*) ::eek:perator delete);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
....
 
A

Alf P. Steinbach

* Nindi:
On comp.lang.c++.moderated
the following question was posted [...]

I am not subscribed there yet .. so couldn't post there.

You do not have to "subscribe" to Usenet groups in order to post in
them, although particular client software may make it seem that way.

However, currently, as of 13th November 2006, since sometime yesterday,
the clc++m moderation server is down, which means that postings will be
delayed or worse. Depending on the mail server used to post, how long
this problem persists, and what the problem really is, postings to
clc++m may simply disappear.

So, wait until you see the first new posting appear in clc++m, then try
to post an on-topic message there, e.g. via Google groups.


Hope this helps,

- Alf (moderator clc++m, but a /long/ way away from that server!)
 
N

Nindi

Shelyag said:
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
...




But what if there is a throw between populating the vector and the
for_each statement.. surly it is better to use a smart pointer .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top