title: Generate function pointer declaration at run-time in C++?
body: <p>Is it possible to create/generate a pointer declaration similar to:</p>
  
  <pre><code>void (*foo)(int, float);
  
  bool (*foo)();
  
  char (*foo)(char, int);
  </code></pre>
  
  <p>But without knowing the type of the arguments or the return type until run-time.</p>
  
  <p>The function declaration would be read from a string which would specify the return and argument type (if any) then (if possible) stored in a c++ container.</p>
  
  <p>Can it be done at run-time (not compile-time) ? And also C++11 can be used if necessary.</p>
  
  <p>I really doubt this can be done in a statically typed language like C++ but if it can be done then what approach would someone use. No need for code (but it's appreciated) just some guidance to what must be used.</p>
  
  <p>EDIT:</p>
  
  <p>After testing several ideas it turns out that it can't be achieved (directly) with C++. Luckily I found the <a href="http://dyncall.org/" rel="nofollow">dyncall</a> library which allows me do do it (indirectly) and on a quite large number of platforms.</p>
  
  <p>Example function:</p>
  
  <pre><code>double sqrt(double x);
  </code></pre>
  
  <p>Using dyncall to call the function:</p>
  
  <pre><code>double r;
  DCCallVM* vm = dcNewCallVM(4096);
  dcMode(vm, DC_CALL_C_DEFAULT);
  dcReset(vm);
  dcArgDouble(vm, 4.2373);
  r = dcCallDouble(vm, (DCpointer)&amp;sqrt);
  dcFree(vm);
  </code></pre>
  
  <p>Strings can also be used to declare the structure of a function.</p>
  
  <pre><code>C function prototype                                dyncall signature
  
  void f1();                                          ")v"
  int f2(int, int);                                   "ii)i"
  long long f3(void*);                                "p)L"
  void f3(int**);                                     "p)v"
  double f4(int, bool, char, double, const char*);    "iBcdZ)d"
  </code></pre>