468,457 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

is 'operator delete' a function?

On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

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

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

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

Nov 13 '06 #1
3 1374
* 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!)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 13 '06 #2
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
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);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
....
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

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<TtheInnerPtr;
};
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.
Nov 13 '06 #3

Shelyag Yuriy wrote:
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
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);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
...
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

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

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 .

Nov 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Marcin Vorbrodt | last post: by
20 posts views Thread by Ioannis Vranos | last post: by
3 posts views Thread by Sensei | last post: by
2 posts views Thread by Shark | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.