title: Storing, parsing and calling functions with arbitrary arguments
body: <p>I want to create a C++ library where the user can pass a callback function with arbitrary combinations of arguments of three different types. The library would later, upon receiving some data, parse such data and call the callback with the appropriate arguments.</p>
  
  <p>For instance:</p>
  
  <pre><code>class MyLibrary
  {
  public:
  void registerCallback(std::vector&lt;std::string&gt; patterns, UNKNOWN_TYPE callback)
  {
    this-&gt;patterns = patterns;
    this-&gt;callback = callback;
  }
  
  void parseData()
  {
    for(unsigned int n = 0; n &lt; patterns.size(); ++n)
    {
       DATA data = getDataForPattern(p);
       if(typeIdOfCallbackArgument(callback, n) == typeid(std::string))
         addToCallList(data.asString(), n);
       else if(typeIdOfCallbackArgument(callback, n) == typeid(float))
         addToCallList(data.asFloat(), n);
       else if(typeIdOfCallbackArgument(callback, n) == typeid(std::vector&lt;float&gt;)) 
         addToCallList(data.asVector, n);
    }
    callCallbackWithCallList();
  }
  
  private:
  std::vector&lt;std::string&gt; patterns;
  std::function&lt;UNKNOWN_TYPE&gt; callback;
  type_info typeIdOfCallbackArgument(n);
  template &lt;typename T&gt;
  void addToCallList(T arg, unsigned int position)
  {
   // do some magic to store arbitrary arguments in a callList
  }
  void callCallbackWithCallList()
  {
   // do some magic to call callback() with the arguments stashed in the callList
  }
  };
  </code></pre>
  
  <p>and the user code could look like:</p>
  
  <pre><code>void myCallback(std::string propertyName, float value)
  {
    setProperty(propertyName, value);
  }
  
  MyLibrary lib;
  lib.registerCallback({"aproperty", "avalue"}, myCallback);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>void anotherCallback(std::string propertyName, float value, std::vector&lt;float&gt; vec)
  {
    setPropertyVector(propertyName, value, vec);
  }
  
  MyLibrary lib;
  lib.registerCallback({"aproperty", "avalue", "avector"}, anotherCallback);
  </code></pre>
  
  <p>As I see it, there are three tasks:</p>
  
  <ol>
  <li>"capture" the callback arguments when it is called</li>
  <li>store the arguments for the callback as they are parsed</li>
  <li>call the callback with such arguments</li>
  </ol>
  
  <p>I <em>think</em> that by using a <code>template</code> for the <code>callback</code> argument of <code>registerCallback()</code>, I could use <code>typeid</code> at compile-time, and at runtime deduce from its output the argument types within <code>registerCallback()</code> by demangling the typeid.
  I <em>think</em> I know how to do the function call by storing the callback as a <code>void*</code> and manually setting the registers and stack to pass the arguments to the function, but I'd really want to avoid that (I am on armv7, FWIW).</p>
  
  <p>Are there any C++ features I could use to get this done in a safe and elegant way?</p>
  
  <p>Note that:</p>
  
  <ul>
  <li>I explicitly want to avoid making the callback a C variadic function, in order to make the user code as straightforward as possible. That is: I would want the user code to look as similar as possible to the above.</li>
  <li>the above is a simplified case. In practice I would want to allow the user to register several callbacks for different <code>patterns</code> (i.e.: add more of those <code>lib.registerCallback()</code> lines with different arguments)</li>
  </ul>