Re: When is a destructor "used"?
On 3/26/07 8:05 PM, in article
1174953024.263063.243810@p15g2000hsd.googlegroups.com, "Andrew J. Bromage"
<deguerre@gmail.com> wrote:
G'day.
Thanks for your response, James.
On Mar 26, 7:50 pm, "James Kanze" <james.ka...@gmail.com> wrote:
I don't think that the standard actually addresses this issue.
Obviously, the destructor must be instantiated at some point,
since it will be called by the destructor of Bar.
Sure, but because Container is a template, it's instantiated lazily.
Container's destructor must be instantiated when Bar's destructor is
compiled - so one way or the other Container's destructor is certain to be
instantiated.
The real
question is: if a compiler can determine that a given function
or block of code cannot throw, is it permitted to not require a
definition of code that would only be executed if it did throw?
The real question is slightly more subtle than that.
It's unreasonable to require a C++ compiler to examine how a
member is implemented in order to work out whether or not some
other member of some other class is correct or not. That falls
squarely in the realm of "optimisation".
But there is no issue of "correctness" being raised by this program - there
is nothing that could be at all incorrect with Bar's constructor.
Besides, in this situation it would be incorrect. If the
constructor of Container were not declared as throw-none, then
the compiler couldn't guarantee that the constructor would
never throw. (A specialisation might throw, for example.)
Exception specifications never prevent a function from throwing any kind of
exception that it wants to. A throw exception prevents exceptions from being
caught - it does not prevent them from being thrown. Specifically, if a
function lets an exception that is not allowed by the its exception
specification propagate past its scope, then the program essentially
terminates - thereby preventing any handler in any calling function from
being able to catch the thrown exception.
However, in the case presented here, a compiler can determine
that the constructor of Bar will not throw from the types alone.
The standard states (I forget precisely where) that all
specialisations of Container are constrained to also have their
constructors be throw-none, though I don't recall whether or not
the compiler is required to check this. Most likely not.
Bar's constructor can be declared without or without a throw() specification
regardless of how Container's constructor is declared.
Which means that both behaviors are acceptable.
So is it a defect?
Is what a defect?
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]