C++ Logo


Advanced search

Re: [std-proposals] Relax condition for potentially invoked destructor in constructor

From: Arvid Norberg <arvid.norberg_at_[hidden]>
Date: Sat, 26 Feb 2022 00:30:21 +0100
On Fri, Feb 25, 2022 at 11:36 PM organicoman via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Connor, Arvind
> An object at construction does not call its destructor, in case of
> exception or error.
> A destructor exists only if the instantiated object had successfully been
> constructed.
> Look at this example: (pay attention to the member "TheFree d" of the
> class "test")
> [...]

Please run your examples through compiler explorer, or something similar,
before posting.

The main mistake in your example is that division by zero does not throw an
exception. It's undefined behavior.

If you had bothered to include a return statement in your Throw() function,
your compiler would have told you.

When fixing your code to actually throw an exception, it works as one would

Here's an abbreviated version illustrating the behavior:

You see Arvind? We are already paying the price of keeping record of
> constructed member objects.

No. There's no need for record keeping since the order of construction is
known at compile time, statically. It's encoded in the machine code, not in
any run-time data structure.
So, you don't pay for it (obviously there's the caveat that you pay some
price for exception handling, but I don't think that's what we're talking

Arvid Norberg

Received on 2022-02-25 23:30:34