title: Generic callbacks
body: <p><a href="https://stackoverflow.com/questions/2485058/equivalent-to-window-settimeout-for-c">Extends</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2471529/how-to-implement-generic-callbacks-in-c">Related</a></p>
  
  <p>So, I'm trying to learn template metaprogramming better and I figure this is a good exercise for it.</p>
  
  <p>I'm trying to write code that can callback a function with any number of arguments I like passed to it.</p>
  
  <pre>
  // First function to call
  int add( int x, int y ) ;
  
  // Second function to call
  double square( double x ) ;
  
  // Third func to call
  void go() ;
  </pre>
  
  <p>The callback creation code should look like:</p>
  
  <pre>
  // Write a callback object that
  // will be executed after 42ms for "add"
  Callback&lt;int, int, int&gt; c1 ;
  c1.func = add ;
  c1.args.push_back( 2 );  // these are the 2 args
  c1.args.push_back( 5 );  // to pass to the "add" function
                           // when it is called
  
  Callback&lt;double, double&gt; c2 ;
  c2.func = square ;
  c2.args.push_back( 52.2 ) ;
  
  </pre>
  
  <p>What I'm thinking is, using <em>template metaprogramming</em> I want to be able to declare callbacks like, write a struct like this (please keep in mind this is VERY PSEUDOcode)</p>
  
  <pre>
  &lt;TEMPLATING ACTION &lt;&lt;ANY NUMBER OF TYPES GO HERE&gt;&gt; &gt;
  struct Callback
  {
      double execTime ; // when to execute
      TYPE1 (*func)( TYPE2 a, TYPE3 b ) ;
  
      void* argList ;   // a stored list of arguments
                        // to plug in when it is time to call __func__
  } ;
  </pre>
  
  <p>So for when called with </p>
  
  <pre>
  Callback&lt;int, int, int&gt; c1 ;
  </pre>
  
  <p>You would automatically get constructed for you by &lt; HARDCORE TEMPLATING ACTION &gt; a struct like</p>
  
  <pre>
  struct Callback
  {
      double execTime ; // when to execute
      int (*func)( int a, int b ) ;
  
      void* argList ;   // this would still be void*,
                        // but I somehow need to remember
                        // the types of the args..
  } ;
  </pre>
  
  <p>Any pointers in the right direction to get started on writing this?</p>