Yes, you are right, I am wrong! That works just fine. For the record:<p><pre><code>   template&lt;typename T&gt;
   struct OnScopeExit {
       OnScopeExit(T f) :  f_(f) {};
       ~OnScopeExit() { f_();};
       T f_;
   };

   template&lt;typename T&gt;
   OnScopeExit&lt;T&gt; make_scope_exit(T f) { return OnScopeExit&lt;T&gt;(f); }
</code></pre>
And then you use it as<p><pre><code>    auto x = make_scope_exit([]() { std::cout &lt;&lt; &quot;Exitced Scope!&quot; &lt;&lt; std::endl; });</code></pre>