I actually don&#x27;t think there&#x27;s a nice way of doing this without making the interface slightly more annoying.
If you make OnScopeExit a template on a callable, then you need to give it the type of the callable explicitly on construction, which means that using a lambda becomes more annoying. I.e., you&#x27;d have to do it like this:<p><pre><code>    auto f = []() { std::cout &lt;&lt; &quot;Exited Scope!&quot; &lt;&lt; std::endl; };
    OnScopeExit&lt;decltype(f)&gt; x(f);
</code></pre>
The way unique_ptr does it is by using type erasure, which requires a virtual function call internally, just like std::function.