title: Dynamic function calls at runtime (va_list)
body: <p>There is a way in C to obtain a dynamic length argument list with va_list, as described here:
  <a href="http://www.cprogramming.com/tutorial/c/lesson17.html" rel="nofollow">http://www.cprogramming.com/tutorial/c/lesson17.html</a></p>
  
  <p>That quite simple, but most times in C++ not needed. I am currently building a top level wrapper class for encapsulating some Zend functionality. </p>
  
  <p>Anyway, I do need to make a call to such a dynamic function <strong>like</strong> printf from a normal function dynamically. </p>
  
  <p>I mean the reverse way of the example described above, here is waht I got so war, maybe this explains my idea a little better:</p>
  
  <pre><code>void ZendParams::setRequired( int &amp;Var  )
  {
          // save every var pointer to a stack
          // and save the type with it. (if needed, does not seems to be)
          // after call to ZendParams::fetch()
          // the values are stored in the passed variables
          this-&gt;_params.push_back( (void*) &amp;Var );
          this-&gt;_paramStr.append( 'i' );
  }
  
  size_t ZendParams::fetch()
  {
          if ( zend_parse_parameters(
                  ZEND_NUM_ARGS() TSRMLS_CC, ,
                  this-&gt;_paramStr.c_str(),
                  ...
                  ) !== FAILURE)
          {
  
          }
  
          return 0;
  }
  </code></pre>
  
  <p>So I want to make the call to zend_parse_parameters dynamically.</p>
  
  <p>The base idea is to add pointer to vars in a list and pass them to the zend function (as reference) dynamically.</p>
  
  <p>I thought there must be a way to do this with va_list , too.</p>
  
  <p>But how?</p>
  
  <p>To get it more simple, I am using this example:</p>
  
  <pre><code>list&lt;int&gt; myList;
  myList.push_back(1);
  myList.push_back(5);
  myList.push_back(10);
  myList.push_back(37);
  myList.push_back(42);
  
  double function avg( int num, ... )
      va_list arguments;                     
      int sum = 0;
  
      va_start ( arguments, num );           
      for ( int x = 0; x &lt; num; x++ )        
      {
          sum += va_arg ( arguments, int ); 
      }
      va_end ( arguments );
  
      return sum / num;      
  }
  </code></pre>
  
  <p>I want to call <code>avg</code> with all numbers I got in the list. I took the example function from the tutorial mentioned above, but it should show up what I mean in a very simple way.
  However, I can not change the function called, as it is part of the zend framework.</p>
  
  <p>Is there any way in C or C++ to do this?</p>
  
  <hr>
  
  <p>My 2nd approach:</p>
  
  <pre><code>template &lt;typename... Arguments&gt;
  size_t Zend::getMethodParams( string paramStr, Arguments... Args )
  {    
          if ( zend_parse_parameters(
                  ZEND_NUM_ARGS() TSRMLS_CC, ,
                  paramStr.c_str(),
                  Args...
                  ) == FAILURE)
          {
              return 0;
          }
  }
  </code></pre>
  
  <p>To be called like this (to get the defined 3 Parameters):</p>
  
  <pre><code>string My1stParam;
  int My2ndParam;
  int My3rdParam;
  
  Zend::getMethodParams&lt;string, int, int&gt;( "sii", &amp;My1stParam, &amp;My2ndParam, &amp;My3rdParam );
  </code></pre>
  
  <p>I think that should work, but there is one hard issue with that:
  The zend_parse_parameters function returns 2 values for a string (c-style string!):
  - a char pointer and
  - the string length.</p>
  
  <p>So I would either have to call it that way:</p>
  
  <pre><code>char* My1stParam_str;
  int My1stParam_length
  int My2ndParam;
  int My3rdParam;
  
  Zend::getMethodParams&lt;string, int, int&gt;( "sii", &amp;My1stParam_str, &amp;My1stParam_length, &amp;My2ndParam, &amp;My3rdParam );
  string My1stParam;
  My1stParam.assign(My1stParam_str, My1stParam_length);
  </code></pre>
  
  <p>Anyway, that was what I wanted to prevent.</p>
  
  <p>Or I would have to modify the list of arguments passed to the zend_parse_parameters function to do these additional steps internally. </p>
  
  <p>I am hoping to be able to call it at least in that way:</p>
  
  <pre><code>string My1stParam;
  int My2ndParam;
  int My3rdParam;
  
  Zend::getMethodParams&lt;string, int, int&gt;( "sii", &amp;My1stParam, &amp;My2ndParam, &amp;My3rdParam );
  </code></pre>
  
  <p><strong>So say this clearly: The parameters are known at compile time, but the function call will be very different within all occurrences in the later sourcecode.</strong></p>