title: Is there a way to store different function pointer types in a container
body: <p>In the below code, I'm trying to store function pointers in a vector but each function pointer has a different signature.</p>
  
  <p>The idea of the below code is that when I load a DLL, I simply use a loop to load all functions from the DLL into the vector. Then when I want to call a function, I just call it by name or index and pass a variable amount of arguments. But I really want to call the functions the same way that I loaded them: via a loop.</p>
  
  <pre><code>#if defined _WIN32 || defined _WIN64
  #include &lt;windows.h&gt;
  #else
  #include &lt;dlfcn.h&gt;
  #endif
  
  #include &lt;iostream&gt;
  #include &lt;cstdint&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  
  class Library
  {
      private:
          void* Module;
  
      public:
          Library(std::string Library);
          ~Library();
  
          template&lt;typename T&gt;
          T AddressOf(std::string FunctionName);
  
          template&lt;typename T&gt;
          bool AddressOf(T &amp;FunctionDefinition, std::string FunctionName);
  
          template&lt;typename T, typename ...Args&gt;
          auto CallFunction(void* Function, Args... args) -&gt; decltype(reinterpret_cast&lt;T&gt;(Function)(args...));
  };
  
  Library::Library(std::string Library)
  {
      #if defined _WIN32 || defined _WIN64
      this-&gt;Module = LoadLibrary(Library.c_str());
      #else
      this-&gt;Module = dlopen(Library.c_str(), RTLD_LAZY);
      #endif
  }
  
  Library::~Library()
  {
      #if defined _WIN32 || defined _WIN64
      FreeLibrary(static_cast&lt;HMODULE&gt;(this-&gt;Module));
      #else
      dlclose(this-&gt;Module);
      #endif
  }
  
  template&lt;typename T&gt;
  T Library::AddressOf(std::string FunctionName)
  {
      #if defined _WIN32 || defined _WIN64
      return reinterpret_cast&lt;T&gt;(GetProcAddress(static_cast&lt;HMODULE&gt;(this-&gt;Module), FunctionName.c_str()));
      #else
      return reinterpret_cast&lt;T&gt;(dlsym(this-&gt;Module, FunctionName.c_str()));
      #endif
  }
  
  template&lt;typename T&gt;
  bool Library::AddressOf(T &amp;FunctionDefinition, std::string FunctionName)
  {
      return (FunctionDefinition = this-&gt;AddressOf&lt;T&gt;(FunctionName));
  }
  
  template&lt;typename T, typename ...Args&gt;
  auto Library::CallFunction(void* Function, Args... args) -&gt; decltype(reinterpret_cast&lt;T&gt;(Function)(args...))
  {
      return reinterpret_cast&lt;T&gt;(Function)(args...);
  }
  
  
  
  
  
  std::vector&lt;void*&gt; Functions;
  
  typedef void (*Message)(const LPCSTR sometext);
  typedef void (*MessageEx)(const LPCSTR sometext, const LPCSTR title);
  typedef int (*Add)(int X, int Y);
  typedef int (*Subtract)(int X, int Y);
  
  int main()
  {
      Library L("TestDll.dll");
      std::array&lt;std::string, 4&gt; List = {"Message", "MessageEx", "Add", "Sub"};
  
      /** Get Function Addresses.. **/
      for (std::size_t I = 0; I &lt; List.size(); ++I)
      {
          Functions.push_back(L.AddressOf&lt;void*&gt;(List[I]));
      }
  
      /** Call Functions.. **/
      L.CallFunction&lt;Message&gt;(Functions[0], "Hello World!");
      L.CallFunction&lt;MessageEx&gt;(Functions[1], "Hello World!", "DLL MessageBox Title");
  
      std::cout&lt;&lt;L.CallFunction&lt;Add&gt;(Functions[2], 5, 7)&lt;&lt;"\n";
      std::cout&lt;&lt;L.CallFunction&lt;Subtract&gt;(Functions[3], 7, 5)&lt;&lt;"\n";
  
      return 0;
  }
  </code></pre>
  
  <p>Is there a better way of re-writing my CallFunction class member or a way to retain function signature somehow or Map the name of the function to its signature? I don't mind writing out the typedefs but I hate having to put them as the template argument so that it can cast to it so if I could somehow have the vector store different function signatures would be best but any solutions are welcomed.</p>
  
  <p><strong>EDIT:</strong> To make things clear, I want to somehow figure out the function signature given its arguments. Ex:</p>
  
  <pre><code>//If I do:
  
  CallFunction&lt;int&gt;(FuncPtr, "Subtract", 10, 5);
  
  //It would do:
  
  
  template&lt;typename T, typename ...Args&gt;
  auto Library::CallFunction(void* Function, Args... args) -&gt; decltype(reinterpret_cast&lt;T&gt;(Function)(args...))
  {
      return  //using args, figure out the function signature, call it, and return int.
  }
  </code></pre>