On Wed, Aug 4, 2021, 9:31 AM Jens Maurer via Std-Proposals <std-proposals@lists.isocpp.org> 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`, or maybe replace `unique_ptr` with `shared_ptr`, in order to make this behave correctly.

You could save one more character by changing `void*` to `auto`.

Arthur