function-try-block specification

function-try-block specification

class Base
{
public:
Base()
{
std::cout << "Base constructor." << std::endl;
throw 10;
}
~Base()
{
std::cout << "Base destructor." << std::endl;
}
};

class Base1
{
public:
Base1()
{
std::cout << "Base1 constructor." << std::endl;
}
~Base1()
{
std::cout << "Base1 destructor." << std::endl;
}
};

class Derived : public Base, public Base1
{
public:
Derived();
~Derived();
};

Derived::Derived()
try
{
std::cout << "Derived constructor." << std::endl;
}
catch(...)
{
std::cout << "It is constructor handler!" << std::endl;
}

Derived::~Derived()
{
std::cout << "Derived destructor." << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
Derived derived;

return 0;
}

this code print next word sequence:

Base constructor.
It is constructor handler!
Derived destructor. <--- !call destructor on absent object
Base1 destructor. <--- !call destructor on absent object
Base destructor. <--- !call destructor on absent object
Press any key to continue

What does it mean???

5 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

C++ Standart 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.

First this is VS .NET and icl 7.0 compilers' output. MS compiler does not follow the Standard 15.3.16:

The exception being handled is rethrow if control reaches the end of a handler of the function-try-block of
a constructor or destructor. Otherwise, a function returns when control reaches the end of a handler for the
function-try-block (6.6.3). Flowing off the end of a function-try-block is equivalent to a return with no
value; this results in undefined behavior in a value-returning function (6.6.3).?

MS compiler does not call the rethrow when control reaches the end of destructor or constructor. That is why some extra destructor being called.

If you change the code like:
int _tmain(...)
{
try {
Derived derived;
} catch (...) {
std::cout <<"here" << std::endl;
}
return 0;
}

The correct result should be:(gcc and icc)

Base constructor.
It is constructor handler!
here.

You also mention C++ Standard 15.3.11 which I am not sure what do you mean. For this example:
In main: We first call constructor Derived, in the constructor Derived we call constructor Base, then throw, we do not have fully constructed base classes yet before get into catch handler, so there is no object need to be destroyed before entering the catch handler of a function_try_block.

Hope this answers your question.

Thanks for answers :)

[...skipped...]
>>The correct result should be:(gcc and icc)
Is icc compiler? Is it abbreviature of a compiler?

>>You also mention C++ Standard 15.3.11 which I am not >>sure what do you mean. For this example:
[...skipped...]
I want tell what destructors must call only for FULL constructed objects (my sample doesn't contain complete constructed objects).

p.s. Unfortunately MS.NET and ICL7.0 doesn't like function-try-block constructions... :(

"icc" is Intel Linux compiler.

I was using MS.net and our Intel compiler 8.0 to compile your example that I got same result as you. This is just because we are trying to be comparable with MS compiler on NT side.

BTW: There are bugs in our Intel compiler.:-) That maybe why you say our compile doesn?t like the function-try-block. Please send us bugs, if you see so. We very appreciate.

Melden Sie sich an, um einen Kommentar zu hinterlassen.