I need to implement a functor that takes any (!) function pointer when instantiated, analyses the argument types, stores the pointer and when operator() is called, does some
To be type-safe, Foo should also encode the type of the arguments. The question doesn't specify the use-case fully, so I'm not sure if it is required to be able to pass 'typeless' Foos around and still have each one somehow remember (at runtime) what the types of its arguments are. Anyway, this code will work with the example given in the qeustion.
#include
template
struct Foo {
FunctionType f;
Foo(FunctionType f_) : f(f_) {}
template
void operator() (Args&&... args) {
f( std::forward(args)... );
}
};
template
Foo foo(FunctionType f) {
return Foo(f);
}
void func1(double *, double *, double *)
{ //do something
}
int main() {
auto x = foo(func1);
double d[3] = {2,3,4};
func1(d, d, d);
x(d, d, d);
}