jeudi 13 août 2015

How can implement dynamic function call with C++11 and C++14?

Here is code that I hope explains what I want to achieve.

vector<int> ints;
vector<double> doubles;


struct Arg {
  enum Type {
    Int,
    Double
  };

  Type type;
  int index;
};

template <typename F> 
void Call(const F& f, const vector<Arg>& args) {
  // TODO: 
  //  - First assert that count and types or arguments of <f> agree with <args>.
  //  - Call "f(args)"
}

// Example:

void copy(int a, double& b) {
  b = a;
}

int test() {
  Call(copy, {{Int, 3}, {Double, 2}}); // copy(ints[3], double[2]);
}

Can this be done in C++11 ?
If yes, can the solution be simplified in C++14 ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire