0

I want to create a class with a method

class Caller
{
public:
    void Invoke(const char * function_name, ...);
};

so that if I can call a variety of functions with a variable number and types of argument

void MyFunc(Caller * caller, std::vector<int> const & data)
{
  
    caller->Invoke("doSomething", 10);
    caller->Invoke("doSomethingElse", 10, 1.0f, data);
}

where the functions elsewhere are

void doSomething(int arg) {...}
void doSomethingElse(int x, float y, std::vector<int> const & z) {...}

So my 2 problems are

  1. How do I store pointers to functions with different signatures in a single table?
  2. How does the Invoke() method pass the required arguments to these functions?
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    If you can narrow the different allowed signatures down to a hand full, you could store a list of `std::variant`. Otherwise, you are going to need type-erasure one way or another. The simplest option would be the use of `std::any` as @JeJo suggested. A more sophisticated solution would be something along the lines of this SO answer: https://stackoverflow.com/a/74409037/12173376 – joergbrech Jun 20 '23 at 13:41
  • 1
    Here is the SO answer from my comment above adapted to your specific question: https://godbolt.org/z/r9a1KhMfr – joergbrech Jun 20 '23 at 14:11
  • https://stackoverflow.com/questions/57622162/get-function-arguments-type-as-tuple – Andrej Podzimek Jun 20 '23 at 14:47

0 Answers0