C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Execute statement when leaving scope

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 12 Dec 2022 10:32:34 +0000
On Mon, Dec 12, 2022 at 10:10 AM Peter Olsson via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> > I also propose that this new expression would be very versatile and
> > that you could use it in conditional code branching as follows:
> >
> > void Func(void)
> > {
> > Handle h = GetResource("monkey");
> >
> > if ( ! h ) return;
> >
> > if ( IsHardwareDevice(h) ) ~ { FreeDevice(h); }
> > else ~ { FreeResource(h) };
> >
> > // The rest of the function goes here
> > }
>
> The problem I see here is that the statements in the if and else parts
> have their own scopes which would exit right away so
> FreeDevice/FreeResource would also get called right away, not at the
> end of the function scope.
>
> My assumption here is that this is just a normal if statement and that
> ~{...} is treated as a statement that adds code to be executed to the
> end of the current scope.


You see the curly tilde symbol ~, well my intention is that you locate
that symbol in the code, and then you look to the left of it to find
the first open curly brace, i.e. {.

In the case of the 'if' statement I gave as an example above, if you
look to the left of the ~, it takes you to the curly bracket that
opens the function (and that's the scope in question).

Received on 2022-12-12 10:32:46