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.