G++ function-try-block discrepencies
Jack Reeves
jack_reeves@ibm.net
Wed Feb 2 12:39:00 GMT 2000
Mike Stump
ÃÂ
Here is the list of discrepancies that I have found so far in
the way function-try-blocks are handled by G++.
ÃÂ
I used the following simple program in various
forms ==================================================== #include
<iostream> #include <stdexcept>
ÃÂ
using namespace std;
ÃÂ
static int count = 0;
ÃÂ
struct T { ÃÂ int i; ÃÂ T() :i(++count) { cout
<< "Making T #" << i << endl; } ÃÂ T(const T& obj)
:i(++count) { cout << "Copying T #" << obj.i << " to T #"
<< i << endl; } ÃÂ ~T() { cout << "Destroying T #"
<< i << endl; } }; ÃÂ int dummyInit(bool
f) { ÃÂ if (f) throw logic_error("Constructor
exception"); ÃÂ return 0; }
ÃÂ
struct Dummy { ÃÂ T tmp; ÃÂ int
i; ÃÂ Dummy(T t, bool f); ÃÂ ~Dummy(); };
ÃÂ
Dummy::Dummy(T t, bool f) try : tmp(t),
i(dummyInit(f)) { ÃÂ cout << "Ctor: f = " << f <<
endl; } catch (logic_error& err) { ÃÂ cout << "Ctor
catch(logic_error): what = " << err.what() << endl; ÃÂ cout
<< "Constructor was passed T #" << t.i << ", f = " << f
<< endl; ÃÂ return; file://###1 }
ÃÂ
Dummy::~Dummy() try { ÃÂ cout << "Dummy::dtor"
<< endl; ÃÂ throw std::logic_error("Destructor
exception"); } catch (logic_error& err) { ÃÂ cout << "Dtor
catch(logic_error): what = " << err.what() <<
endl; }
ÃÂ
int main() { ÃÂ cout << "Declaring a Dummy"
<< endl; ÃÂ try { ÃÂ ÃÂ T tmp; ÃÂ ÃÂ Dummy
obj(tmp, true); ÃÂ ÃÂ cout << "main: 'obj' construction complete"
<< endl; ÃÂ } catch (logic_error& err) { ÃÂ ÃÂ cout
<< "main catch(logic_error): what = " << err.what() <<
endl; ÃÂ ÃÂ return -1; ÃÂ } catch (...) { ÃÂ ÃÂ cout
<< "main catch(...)" << endl; ÃÂ ÃÂ return
-1; ÃÂ } ÃÂ return
0; } ================================================================
ÃÂ
A. The Standard says [15.3/11: The fully constructed base
classes and members of an object shall be destroyed before entering the handler
of a function-try-block for the constructor or destructor of that
object.]
ÃÂ
The output of this program shows that the member 'tmp' is
constructed before the exception, but not destroyed before entering the handler
for either the constructor's or the destructor's
function-try-block.
ÃÂ
B. The Standard says [15.3/15: If a return statement appears
in the handler of a function-try-block of a constructor, the program is
ill-formed]
ÃÂ
I assume that the program above should issue a diagnostic for
the return statement at ###1
ÃÂ
C. The Standard says [15.3/16: The exception being handled is
rethrown if control reaches the end of a handler of the function-try-block of a
constructor or a destructor.]
ÃÂ
If we remove the 'return' at ###1, the program demonstrates
that the exception is not rethrown at the end of the constructor's
function-try-block. Nor is it rethrown at the end of the destructor's
function-try-block. The behavior in both cases is as if the handler of the
function-try-block returned.
ÃÂ
This is all I have discovered so far. I will be doing some
more testing in the future. If I haven't mentioned it before, I think it is
really great that G++ supports function-try-blocks. I intend to be using them in
the near future.
ÃÂ
Jack Reeves
ÃÂ
ÃÂ
More information about the Gcc-bugs
mailing list