C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 23 Dec 2022 09:55:18 +0000
On Monday, December 12, 2022, Frederick Virchanza Gotham wrote:

void Func(void)
> {
> Handle h = GetResource("monkey");
>
> if ( ! h ) return;
>
> ~ { FreeDevice(h); }
>
> // The rest of the function goes here
> }
>


Does anyone like my minimalistic simplistic syntax here? You don't need to
include a header file nor write lambda syntax. Perhaps we could also make
use of the 'throw' keyword to indicate how the scope was exited:

void Func(void)
{
    Handle h = GetResource("monkey");

    if ( ! h ) return;

    ~ { if throw FreeDevice(h); }

    // The rest of the function goes here
}

In the above code snippet, FreeDevice only gets called if Func is exited by
means of a thrown exception. The two keywords 'if throw' side by side would
have a special meaning here.

Received on 2022-12-23 09:55:20