0

I'm currently writing a simulation software. I have a relatively complex operator to compute, with several parameters. One of the parameters can be a user-specified, nonlinear function. Something like:

double op(double param1, double param2, ..., function<double(double)> fct){
   ...
}

Since the function will be called several times, I would like to enable the compiler to inline the function. Fortunately, the function can be hardcoded when calling op. Sou I will always have something like op(1.0, 2.0, ..., nonlinear1) where nonlinear1 is the name of the function and not a variable or something.

I was thinking about two ideas:

  1. Function pointer double (*)(double): Won't work.
  2. Template parameter:

As follows:

template <function<double(double)> FCT> 
double op(double param1, double param2, ...){
   ...
   double a = FCT(param2);
   ...
}

This seems to work at a first glance, but does the compiler actually know the function behind the FCT object or does it just see a function pointer and return/argument specification?

How else could I achieve what I want?

Michael
  • 7,407
  • 8
  • 41
  • 84

1 Answers1

0

You may use normal template parameter like:

template <typename FUNCTOR>
double op(double param1, double param2, ..., FUNCTOR fct){
   ...
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302