C++ Logo

std-proposals

Advanced search

Re: scope guard

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 3 Aug 2021 15:56:20 +0300
On 8/3/21 3:31 PM, Nikolay Mihaylov wrote:
> Do we really need this to be standardized?

Yes. Preferably with less syntactical noise than this.

There are much simpler, and possibly less frequently used boilerplate
components standardized.

> https://gcc.godbolt.org/z/6zTMPPWbz <https://gcc.godbolt.org/z/6zTMPPWbz>
>
> #include <cstdio>
>
> template<class E>
> struct ScopeExit{
> ScopeExit(E e) : e(e){}
>
> ~ScopeExit(){
> e();
> }
>
> private:
> E e;
> };
>
> int main(){
> ScopeExit guard([](){ printf("hi!\n"); });
> }
>
>
>
> On Tue, Aug 3, 2021 at 3:22 PM Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
> On 8/3/21 3:08 PM, Jens Maurer via Std-Proposals wrote:
> > On 03/08/2021 11.46, Baruch Burstein via Std-Proposals wrote:
> >> Hi.
> >> Why is there no generic scope guard in the standard library?
> >> I am sure there must have been such a proposal before, probably
> with an explanation of why it was not accepted, but I couldn't find it.
> >
> > Try std::unique_ptr with a custom deleter.
>
> This only works for pointers. I wish Boost.ScopeExit was transformed
> into a language feature.
>
> https://www.boost.org/doc/libs/1_76_0/libs/scope_exit/doc/html/index.html
> <https://www.boost.org/doc/libs/1_76_0/libs/scope_exit/doc/html/index.html>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>
>

Received on 2021-08-03 07:56:25