title: ScopedExit implementation: pass arguments to the function object
body: <p>I'm trying to implement simple ScopedExit class. Here's the code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  template&lt;class R, class... Args&gt;
  class ScopedExit
  {
  public:
      ScopedExit(std::function&lt;R(Args...)&gt; exitFunction)
      {
          exitFunc_ = exitFunction; 
      }
  
      ~ScopedExit()
      {
          exitFunc_();
      }
  private:
      std::function&lt;R(Args...)&gt; exitFunc_;
  };
  
  template&lt;&gt;
  class ScopedExit&lt;void&gt;
  {
  public:
      ScopedExit(std::function&lt;void ()&gt; exitFunction)
      {
          exitFunc_ = exitFunction; 
      }
  
      ~ScopedExit()
      {
          exitFunc_();
      }
  private:
      std::function&lt;void ()&gt; exitFunc_;
  };
  
  void foo()
  {
      std::cout &lt;&lt; &quot;foo() called\n&quot;;
  }
  
  class Bar
  {
  public:
      void BarExitFunc(int x, int y)
      {
          std::cout &lt;&lt; &quot;BarExitFunc called with x =&quot; &lt;&lt; x &lt;&lt; &quot;y = &quot; &lt;&lt; y &lt;&lt; &quot;\n&quot;;
      }
  };
  
  int main()
  {
      Bar b;
      std::cout &lt;&lt; &quot;Register scoped exit func\n&quot;;
      {
          ScopedExit&lt;void, int, int&gt; exitGuardInner(std::bind(&amp;Bar::BarExitFunc, &amp;b, 18, 11));
      }
      ScopedExit exitGuardOutter(foo);
      std::cout &lt;&lt; &quot;About to exit from the scope\n&quot;;
      return 0;
  }
  </code></pre>
  <p>So, there are a couple of questions:</p>
  <ol>
  <li><p>How to pass exit's function arguments to it? For example, I bind BarExitFunc with two integer arguments: 18 and 11. So how can I pass it to the exitFunc_ in the destructor? I think I need something like invoke function with std::forward&lt;&gt;.</p>
  </li>
  <li><p>gcc 4.7.2 (from ideone.com) complains about exitGuardOutter. It says:</p>
  </li>
  </ol>
  <blockquote>
  <p>prog.cpp:60:16: error: missing template arguments before ‘exitGuardOutter’</p>
  <p>prog.cpp:60:16: error: expected ‘;’ before ‘exitGuardOutter’</p>
  </blockquote>
  <p>Thanks in advance.</p>