title: c++: call a function with with various number of arguments stored in an array?
body: <p>I've got a function with signature <code>double func(double,...)</code> and I'm sure all the arguments I'm passing to func are type double. All arguments are stored in some a vector called arguments, I'm just wondering if there is way to write a generic code to pass any number of arguments specified in argumentVector to func?</p>
  
  <p>something like :</p>
  
  <pre><code>for (int i=0;i&lt;agumentVector.size();i++)
      pushstack(argumentVector[i]);
  res = call(func);
  </code></pre>
  
  <p>so far I've tried this code but it seems to have problems:</p>
  
  <pre><code>double* param = &amp;argumentVector[0];
  
  for(int i=0;i&lt;argumentVector.size();i++)
  {
      _asm  sub         esp,8;
      _asm  fld         param;
      _asm  fstp        qword ptr [esp];
      param++;
  
  }
  _asm  call        func;
  _asm  add         esp,10h;
  _asm  fstp        qword ptr res;
  </code></pre>
  
  <p>--just a crazy result I'm getting! this code runs without any problems :</p>
  
  <pre><code>double x[2] = {5,6};
  double *param = x;
  double res;
  
      _asm  sub         esp,8;
      _asm  fld         x;
      _asm  fstp        qword ptr [esp];
      _asm  sub         esp,8;
      _asm  fld         x+8;
      _asm  fstp        qword ptr [esp];
      param++;
  _asm  call        p;
  _asm  add         esp,10h;
  _asm  fstp        qword ptr res;
  
  cout &lt;&lt; res &lt;&lt; "\n";
  </code></pre>
  
  <p>while in this one arguments are not passed properly!</p>
  
  <pre><code>double x[2] = {5,6};
  double *param = x;
  double res;
  
      _asm  sub         esp,8;
      _asm  fld         param;
      _asm  fstp        qword ptr [esp];
      _asm  sub         esp,8;
      _asm  fld         param+8;
      _asm  fstp        qword ptr [esp];
      param++;
  _asm  call        p;
  _asm  add         esp,10h;
  _asm  fstp        qword ptr res;
  
  cout &lt;&lt; res &lt;&lt; "\n";
  </code></pre>
  
  <p>do you have any idea why? I though both x and param were simple arrays!</p>
  
  <p><strong>--edit2--</strong></p>
  
  <p>here is the complete assembly code so far VC generates</p>
  
  <pre><code>    double x[2] = {5,6};
  010E1A05  fld         qword ptr [__real@4014000000000000 (10EB928h)]  
  010E1A0B  fstp        qword ptr [ebp-48h]  
  010E1A0E  fld         qword ptr [__real@4018000000000000 (10EB878h)]  
  010E1A14  fstp        qword ptr [ebp-40h]  
      double *param = x;
  010E1A17  lea         eax,[ebp-48h]  
  010E1A1A  mov         dword ptr [ebp-54h],eax  
      double res;
      p(param[0],param[1]);
  010E1A1D  mov         esi,esp  
  010E1A1F  mov         eax,dword ptr [ebp-54h]  
  010E1A22  sub         esp,8  
  010E1A25  fld         qword ptr [eax+8]  
  010E1A28  fstp        qword ptr [esp]  
  010E1A2B  mov         ecx,dword ptr [ebp-54h]  
  010E1A2E  sub         esp,8  
  010E1A31  fld         qword ptr [ecx]  
  010E1A33  fstp        qword ptr [esp]  
  010E1A36  call        dword ptr [ebp-14h]  
  010E1A39  fstp        st(0)  
  010E1A3B  add         esp,10h  
  010E1A3E  cmp         esi,esp  
  010E1A40  call        @ILT+795(__RTC_CheckEsp) (10E1320h)  
  
      {
          _asm  sub         esp,8;
  010E1A45  sub         esp,8  
          _asm  fld         param+8;
  010E1A48  fld         dword ptr [ebp-4Ch]  
          _asm  fstp        qword ptr [esp];
  010E1A4B  fstp        qword ptr [esp]  
  
          _asm  sub         esp,8;
  010E1A4E  sub         esp,8  
          _asm  fld         param;
  010E1A51  fld         dword ptr [ebp-54h]  
          _asm  fstp        qword ptr [esp];
  010E1A54  fstp        qword ptr [esp]  
  
      }
      _asm  call        p;
  010E1A57  call        dword ptr [ebp-14h]  
      _asm  add         esp,10h;
  010E1A5A  add         esp,10h  
      _asm  fstp        qword ptr res;
  010E1A5D  fstp        qword ptr [ebp-64h]  
  
      cout &lt;&lt; res &lt;&lt; "\n";
  010E1A60  push        offset string "\n" (10EB834h)  
  010E1A65  mov         esi,esp  
  010E1A67  sub         esp,8  
  010E1A6A  fld         qword ptr [ebp-64h]  
  010E1A6D  fstp        qword ptr [esp]  
  010E1A70  mov         ecx,dword ptr [__imp_std::cout (10EF3A8h)]  
  010E1A76  call        dword ptr [__imp_std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::operator&lt;&lt; (10EF3ACh)]  
  010E1A7C  cmp         esi,esp  
  010E1A7E  call        @ILT+795(__RTC_CheckEsp) (10E1320h)  
  010E1A83  push        eax  
  010E1A84  call        std::operator&lt;&lt;&lt;std::char_traits&lt;char&gt; &gt; (10E1294h)  
  010E1A89  add         esp,8  
      return 0;
  </code></pre>