K
Klaus Ahrens
if arrays were exception save (as i thought) the following should give
2 destructors for the partly constructed x2. unfortunately none of the
compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.
when using the (commented) Xs-ctor with try-rethrow at least g++ and
vc++.net show really two destructors.
even worse, when taking the vector-based Xsv instead if Xs in main
there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.
very puzzled:
k ahrens
//-------------------------------------------------------------------
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
using std::vector;
class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};
int X::c=2;
class Xs {
int n;
X* data;
public:
Xs(int i):n(i), data(new X){}
// Xs(int i) try :n(i), data(new X){}catch(...){throw;}
~Xs(){ delete[] data; }
};
class Xsv {
int n;
vector<X> *data;
public:
Xsv(int i):n(i), data(new vector<X>(i)){}
// Xsv(int i) try :n(i), data(new vector<X>(i)){}
// catch(...){throw;}
~Xsv(){ delete data; }
};
int main()
{
{Xs x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
using std::vector;
class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};
int X::c=2;
class Xs {
int n;
X* data;
public:
// Xs(int i):n(i), data(new X){}
Xs(int i) try :n(i), data(new X){}catch(...){throw;}
~Xs(){ delete[] data; }
};
class Xsv {
int n;
vector<X> *data;
public:
// Xsv(int i):n(i), data(new vector<X>(i)){}
Xsv(int i) try :n(i), data(new vector<X>(i)){}
catch(...){throw;}
~Xsv(){ delete data; }
};
int main()
{
{Xsv x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------
2 destructors for the partly constructed x2. unfortunately none of the
compilers i tried (g++3.3.1, vc++.net, bcc32, icc) conforms to this.
when using the (commented) Xs-ctor with try-rethrow at least g++ and
vc++.net show really two destructors.
even worse, when taking the vector-based Xsv instead if Xs in main
there is only ONE dtor-call and two with the try-rethrowing Xsv-ctor.
very puzzled:
k ahrens
//-------------------------------------------------------------------
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
using std::vector;
class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};
int X::c=2;
class Xs {
int n;
X* data;
public:
Xs(int i):n(i), data(new X){}
// Xs(int i) try :n(i), data(new X){}catch(...){throw;}
~Xs(){ delete[] data; }
};
class Xsv {
int n;
vector<X> *data;
public:
Xsv(int i):n(i), data(new vector<X>(i)){}
// Xsv(int i) try :n(i), data(new vector<X>(i)){}
// catch(...){throw;}
~Xsv(){ delete data; }
};
int main()
{
{Xs x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#include <iostream>
#include <vector>
using std::cerr;
using std::endl;
using std::vector;
class X {
static int c;
public:
X(){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
X(const X&){ cerr<<"X("<<c<<")"<<endl; if (!c--) throw 0; }
~X() { ++c; cerr<<"~X()"<<endl;;}
};
int X::c=2;
class Xs {
int n;
X* data;
public:
// Xs(int i):n(i), data(new X){}
Xs(int i) try :n(i), data(new X){}catch(...){throw;}
~Xs(){ delete[] data; }
};
class Xsv {
int n;
vector<X> *data;
public:
// Xsv(int i):n(i), data(new vector<X>(i)){}
Xsv(int i) try :n(i), data(new vector<X>(i)){}
catch(...){throw;}
~Xsv(){ delete data; }
};
int main()
{
{Xsv x2(4);} // resp. Xsv...
}
//-------------------------------------------------------------------