C++ Logo


Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 12 Dec 2022 15:13:40 -0500
On Mon, Dec 12, 2022 at 3:06 PM Lénárd Szolnoki via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Hi,
> On 12 December 2022 09:58:22 GMT, Ville Voutilainen via Std-Proposals <std-proposals_at_[hidden]> wrote:
> >That's because it's in gcc 13,
> >https://gcc.gnu.org/gcc-13/changes.html
> >
> >
> > - Support for the <experimental/scope> header from v3 of the Library
> > Fundamentals Technical Specification.
> Interesting. I'm not very impressed with the generated code for scope_success:
> https://godbolt.org/z/joYGhjqKa
> I believe that f1 and f2 are equivalent.

They are not equivalent. `scope_success` means "call this function if
the scope is not exiting due to an exception being thrown from within
the scope." `scope_fail` means exiting due to an exception.

The equivalent would be `scope_exit`. That one (here:
https://godbolt.org/z/n3b9oxTKv ) has a bit of extra cruft due to not
being able to do a proper tail call.

That being said, `scope_exit` isn't for cases like that. They're for
cases like this:

int f1()
  auto thingy = ...
  auto ret = something();
  return ret;

Cases where you're dealing with some resource from a non-RAII API that
needs to have some cleanup done, but for whatever reason can't be
wrapped in a RAII API.

Received on 2022-12-12 20:15:31