title: Searching for a trick to avoid creating a field in a class template
body: <p>I have a scope guard like class (this is the simplified test case):</p>
  
  <pre><code>template&lt;void(*close)()&gt;
  struct Guard1
  {
    template&lt;typename O&gt;
    Guard1(O open) { open(); }
  
    ~Guard1() { close(); }
  };
  
  void close() { std::cout &lt;&lt; "close g1\n"; }
  
  int main()
  {
    Guard1&lt;close&gt; g1 = [](){ std::cout &lt;&lt; "open g1\n"; };
  }
  </code></pre>
  
  <p>I modified it such that the close expression can also be given as a lambda:</p>
  
  <pre><code>class Guard2
  {
    std::function&lt;void()&gt; close;
  public:
  
    template&lt;typename O, typename C&gt;
    Guard2(O open, C close) : close(close)
    {
      open();
    }
  
    ~Guard2() { close(); }
  };
  
  
  int main()
  {
    Guard2 g2(
        [](){ std::cout &lt;&lt; "open g2\n"; },
        [](){ std::cout &lt;&lt; "close g2\n"; });
  }
  </code></pre>
  
  <p>However I had to introduce an extra field <code>const std::function&lt;void()&gt;&amp; close;</code> to pass the lambda from the constructor to the destructor.</p>
  
  <p>Is there a way to avoid this extra field while still keeping the lambda (and a nice syntax when used as well)?</p>