C++ Logo

std-proposals

Advanced search

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

From: Peter Olsson <dataapa_at_[hidden]>
Date: Mon, 12 Dec 2022 11:10:13 +0100
> 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.

Received on 2022-12-12 10:10:26