Function as a parameter with non specified number of arguments

Hi everybody!
I'm new to this forum, and I hope i won't make mistakes posting my question.

I got stuck on a problem: how can I pass a function to another function, but without knowing in advance how many variables will the last one have?

I built a program that finds the roots of a particular function, something like:

1
2
3
4
5
void find_roots(double &x, vector<double> &V) {
     double f = 2*x*x;   //for example
     .....find roots of f with a bisection method.....
     .....put the roots in vector V.....
}

I was trying to generalize it, so that i could pass a function as a parameter instead of defining it in the inside.
If I'm not mistaken this can be done in a couple of ways, for example:
1
2
3
void find_roots(double my_function(double), vector<double> &V) {
     .....find roots and put them in V.....
}

or:
1
2
3
void find_roots(double (*my_function)(double), vector<double> &V) {
     .....find roots and put them in V.....
}

Anyway, both want to know how many parameters (and their type) will the function parameter "my_function" have.
Instead, i'd like to pass ANY kind of function, also:
 
f = 2*x+y+z

where y and z are parameters defined in another part of the main() program (not outside main()). For example:
1
2
3
4
5
6
7
8
9
10
11
void find_roots(???, vector<double> V) {
     .....find and put them in V.....
}

int main() {
double y = 5.2;
double z = 1;
vector<double> roots;
find_roots(/*somehow my function*/, V);
return 0;
}

Achieving this, I could reuse the find_roots method in many other occasions.
Is this possible in a simple manner?
Thanks a lot in advance!

P.S. I'm quite new it C++ code, so if there's any smarter way of doing this task i'd be thankful for any suggestion!
Use the polymorphic call wrapper std::function<>
http://en.cppreference.com/w/cpp/utility/functional/function
http://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/

in conjucntion with lambda expressions.
http://msdn.microsoft.com/en-us/library/vstudio/dd293608.aspx


Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <functional>
#include <vector>

void find_roots( std::function< double(double) > my_function, std::vector<double>& V )
{
    // ...
}

int main()
{
    double y = 22 ;
    double z = 0 ;

    const auto f = [y,z]( double x ) { return 2*x+y+z ; } ;
    std::vector<double> roots ;

    find_roots( f, roots ) ;
}
Thanks JLBorges for your reply,
I'm learning about lambda expressions right now, and I'm sure you got the point!

After my post, i was searching online and I found this way of doing it (didn't post if because I was trying to compile without success, but now it works):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<functional>
#include<vector>

using namespace std;

double f(double x, double y) {
  return 2*x*x+y;     //for example
}

void zeros( function<double(double)> ptr, vector<double> &V )   {
       V.push_back(ptr(2));     // for example
}

int main()
{
     vector<double> V;
     zeros( bind( f, placeholders::_1, 20 ), V );
     cout << V[0] << endl;   //output is 28 as expected
}

that uses function<> as you suggested, and placeholder::_n
Is this a good way as well?
> Is this a good way as well?

Yes.

Make a local decision on a case to case basis; use whatever seems more appropriate.
Lambda expressions seem to be simpler and more elegant, I'll try both the ways.
Thank you very much for your suggestion!
Topic archived. No new replies allowed.