Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending the name of a function to a function

What I want to do is:

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average( MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + data[i].MyFunction(double a, double b)

            return sum / n;
    }

}

example:

double average( max, double a, double b)
{
    double sum = 0;
    int n = data.size();

    for ( int i = 0; i < n; i++)
        sum = sum + data[i].max(double a, double b)

        return sum / n;
}

Why?

  1. it would save me to write function like: average of sum. average of max. average of min which are all pretty similar functions.
  2. the way it is coded B< B< A> > works

What have I tried?

  • function pointer
  • S1:

      typedef double (A::*MyFunctionf)(double, double);
      typedef double (B<A>::*MyFunctionff)(double, double);
      typedef double (B<B<A> >::*MyFunctionfff)(double, double);
    
    • it Works. Problems:
      • it is not beautiful to declare 3-4 typedef of function pointer
      • if I want to write the function inside B that sent a function pointer it will be hard coded and only 1 of the 3 typedef can be hard coded. Meaning: it is not working for every cases
  • S2 (based on Template typedefs - What's your work around?):

      template <typename rtype, typename t>
      struct CallTemplate
      {
        typedef rtype (t::*ptr)(double, double);
      };
    
     // the function becomes :
     template <typename T>
     template<typename rtype>
     double B<T>::average(CallTemplate<double, T> MyFunction, double a, double b)
     {
        double sum = 0;
       int n = data.size();
    
          for ( int i = 0; i < n; i++)
            sum = sum + (data[i].*MyFunction)( a, b)
    
        return sum / n;
      }
    

    example:

     // makes an error "Myfunction was not declared" + " 
     // dependent-name'{anonymous}::CallTemplate<double, A>::ptr' 
     // is parsed as a non-type, but instantiation yields a type"
     CallTemplate<double, A>::ptr MyFunction = &A::max; 
     Average(max, t, v);
    

I do not know where the problem comes from. I have also tried Boost.Function

like image 916
Setepenre Avatar asked Sep 22 '13 13:09

Setepenre


People also ask

How do you pass a function to another function?

Functions can be passed into other functionsFunctions, like any other object, can be passed as an argument to another function. >>> def greet(name="world"): ... """Greet a person (or the whole world by default).""" ... print(f"Hello {name}!") ... >>> greet("Trey") Hello Trey!

How do you call function name?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method.

What do you call the information passed to a function?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.


1 Answers

Yes it's possibile.

You're looking for member pointers. The syntax is not obvious however:

struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() { return x + y; }
    double max() { return std::max(x, y); }
};

This is a class that defines a couple of methods (sum and max).

template <typename T>
struct B
{
    std::vector<T> data;
    double average(double (T::*MyMethod)())
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + (data[i].*MyMethod)();
        return sum / n;
    }
};

This is a class with a method that accepts a method pointer and that will compute the average of the result of calling the pointed method over the elements of a vector.

An example of passing the two A methods is:

int main(int argc, const char *argv[]) {
    B<A> b;
    b.data.push_back(A(1, 2));
    b.data.push_back(A(3, 4));
    b.data.push_back(A(5, 6));
    std::cout << b.average(&A::max) << std::endl;
    std::cout << b.average(&A::sum) << std::endl;
    return 0;
}
like image 150
6502 Avatar answered Oct 17 '22 07:10

6502