<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 4, 2021, 9:31 AM Jens Maurer via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org">std-proposals@lists.isocpp.org</a>&gt; wrote:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
In the interest of clarity, the shortest generic scope guard using<br>
std::unique_ptr that I&#39;ve come up with so far is:<br>
<br>
<br>
template&lt;class X&gt;<br>
using scope_guard = std::unique_ptr&lt;void, X&gt;;<br>
<br>
void f()<br>
{<br>
  scope_guard guard(0, [](void*) { std::cout &lt;&lt; &quot;hi&quot;; });<br>
}<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">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 `&amp;guard`, or maybe replace `unique_ptr` with `shared_ptr`, in order to make this behave correctly.</div><div dir="auto"><br></div><div dir="auto">You could save one more character by changing `void*` to `auto`.</div><div dir="auto"><br></div><div dir="auto">Arthur</div></div>

