C++ Logo

std-proposals

Advanced search

Re: scope guard

From: Peter C++ <peter.cpp_at_[hidden]>
Date: Wed, 4 Aug 2021 20:49:22 +0200
FWIW, the scope_guard/unique_resource paper came with an implementation available on my github account (for C++17)

https://github.com/PeterSommerlad/scope17

regards
Peter

sent from a mobile device so please excuse strange words due to autocorrection.
Peter Sommerlad
peter.cpp_at_[hidden]
+41-79-432 23 32

> On 4 Aug 2021, at 16:48, Jens Maurer via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On 04/08/2021 15.48, Arthur O'Dwyer wrote:
>> On Wed, Aug 4, 2021, 9:31 AM Jens Maurer via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>> wrote:
>>
>>
>> In the interest of clarity, the shortest generic scope guard using
>> std::unique_ptr that I've come up with so far is:
>>
>>
>> template<class X>
>> using scope_guard = std::unique_ptr<void, X>;
>>
>> void f()
>> {
>> scope_guard guard(0, [](void*) { std::cout << "hi"; });
>> }
>>
>>
>> This is pretty neat, actually! But are you sure that unique_ptr calls its deleter when it holds nullptr? I think you need to replace `0` with `&guard`,
>
> Yes, I guess that's correct.
>
>> You could save one more character by changing `void*` to `auto`.
>
> Yes.
>
> Jens
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-08-04 13:49:29