title: Passing a closure as a parameter to a constructor c++
body: <p>In the code below, I'd like to get rid of the weird constructors taking various types of function pointers, and the parameter lists that have to be saved off (along with all the member variables that are required to hold all this), and instead use a closure to do all of that, leaving Event with a single member variable that is something like <code>Closure closure;</code>. Is there a way to do this using closures in C++0x?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;functional&gt;
  #include &lt;algorithm&gt;
  
  class Event
  {
      std::function&lt;double(double, double)&gt; func1;
      bool func1Defined;
      double double1, double2;
  
      std::function&lt;double(int, int, int)&gt; func2;
      bool func2Defined;
      int int1, int2, int3;
  public:
      void setFalse()
      {
          func1Defined = false;
          func2Defined = false;
      }
      Event(std::function&lt;double(double, double)&gt; func, double double1, double double2)
      {
          setFalse();
          func1 = func;
          func1Defined = true;
          this-&gt;double1 = double1;
          this-&gt;double2 = double2;
      }
  
      Event(std::function&lt;double(int, int, int)&gt; func, int int1, int int2, int int3)
      {
          setFalse();
          func2 = func;
          func2Defined = true;
          this-&gt;int1 = int1;
          this-&gt;int2 = int2;
          this-&gt;int3 = int3;
      }
  
      void run()
      {
          double ret = 0;
          if (func1Defined) ret = func1(double1, double2);
          else if (func2Defined) ret = func2(int1, int2, int3);
          /* do something with ret */
          std::cout &lt;&lt; ret &lt;&lt; "\n";
      }
  };
  
  double twoDoubleParam(double a, double b)
  {
      return a + b;
  }
  
  double threeIntParam(int a, int b, int c)
  {
      return (a + b) / c;
  }
  
  int main(int argc, char* argv[])
  {
      std::list&lt;Event&gt; events;
      events.push_back(Event(*twoDoubleParam, 1.0, 3.5));
      events.push_back(Event(*threeIntParam, 2, 4, 2));
      std::for_each(events.begin(), events.end(), [&amp;](Event event)
      {
          event.run();
      });
  
      int y;
      std::cin &gt;&gt; y;
      return 0;
  }
  </code></pre>
  
  <p>Prefer no boost, but if boost is best/only easy way to do this I'd like to know how.</p>