I want to have a type describing a function that would allow creating new functions of the same type by combining existing functions, something like this:
FuncType f;
FuncType g;
FuncType h = f(g);
FuncType e = f + g;
I tried the using function pointers and assigning lambdas to them as follows:
typedef double(*FunPtr)(double);
double Fun1(double a) { return 2 * a; }
double Fun2(double a) { return a * a; }
int main{
auto Fun3 = [](double a){return -a;};
FuncType F1 = Fun1;
FuncType F2 = Fun2;
FuncType F3 = Fun3;
auto HFun = [](double a){return (*F1)(a);} // does not work, requires capturing F1
auto HFun = [F1](double a){return (*F1)(a);} // works
FunPtr H = HFun; //Does not work, I suppose because of capturing F1.
}
Replacing
typedef double(*FunPtr)(double);
with
typedef std::function FunPtr;
solves the issue, but the function calls are going to happen inside very large nested loops, hence performance can be an issue and I have read here and there that using std::function comes with an overhead.
1- Is there a way to make this possible in a way that has a better performance compared to std::function?
2- Does using function pointers have a better performance in the first place?
3- Which of normal functions or lambdas is a better choice to start with? (F3 vs F1)
Thanks a lot!