How to write such function on C ?
function executor (foo, ...args) {
return foo(...args)
}
I don't understand how to declare template on C
CodePudding user response:
Your question is not really clear to me what you are asking, or what you are trying to do.
Here's an example of an invoke
template function that calls the passed function and passes in the arguments to the function's parameters. That seems to be what your code snippet is trying to do.
The code is just for quick-and-dirty example. It is not optimal, because it is not using perfect forwarding of the arguments. Not sure what you want to do with the result of the invoked function, or handle exceptions.
#include <iostream>
using std::cout;
template <typename F, typename... Ts>
void invoke(F fn, Ts... args) {
fn(args...);
}
void print(int a, int b) {
cout << "print:" << a << " " << b << "\n";
}
void bigprint(int a, int b, int c) {
cout << "bigprint:" << a << " " << b << " " << c << "\n";
}
int main() {
invoke(print, 1, 2);
invoke(bigprint, 1, 2, 3);
}