My problem is following, I have a function which takes one function as a parameter. The problem is that in some cases the passed function fEval() needs to be called with two parameters instead of one as fEval(somevalue1,somevalue2)
Func(double (*fEval)(double F1),double min, double max,...)
{
double value1 = fEval(10);
// do something here
double value2 = fEval(20,30);
}
So what would be the correct way to implement Func function ?
I know I can’t do it either
Func(double (*fEval)(double F1),double min, double max,...)
or
Func(double (*fEval)(double F1,double F2),double min, double max,...)
Thanks !
Okay let me rephrase the problem. I need to create a function which could take a one unknown function as a first parameter, two different values and an argument list.
Something like
double Function(RandomFunction, val1, val2, ...);
The random function will be either:
double Func1(double x)
{
m_x = x;
//Calling function
// Set other things
}
double Func2(double x,double y)
{
m_y = y;
m_x = x;
//Calling function
// Set other things
}
I’ll try that functor way but I’m not sure is it right way to do this ? Doesn’t it require me to overload () inside of the possible functions what could be called ?
You probably should require a functor that has
operator()overloaded to take one or two arguments:Other than that (besides passing around variadic functions, which wouldn’t be good because it couldn’t know whether it was being called with one or two arguments) I don’t think there is another good way to have a function that can take both 1 or 2 arguments.
And as ildjarn mentioned in the comments, you could also make it a template so it can take any kind of functor: