Niklas
Niklas

Reputation: 33

c++ pass data source function as parameter

I have a static function:

void E::createEP(std::list<double>* ret, double length, double (*getHeight)(double)) {
  // magic
  double sampleIntervall = //from magic

  double dist = 0;
  while (length - dist > -0.1) {
    ret->push_back(dist, (*getHeight)(dist));
    dist += sampleIntervall;
  }
}

In different calls I have two different functions to pass as getHeight():

Is it somehow possible to pass non-static functions as parameters to a static function?

If not, what would be the most elegant alternative? I don't want to duplicate the entire code in createEP().

Upvotes: 3

Views: 105

Answers (4)

Jarod42
Jarod42

Reputation: 218148

As alternative to std::function

void E::createEP(std::list<double>* ret,
                 double length,
                 const std::function<double (double)>& getHeight)

You may use directly template

template <typename F>
void E::createEP(std::list<double>* ret,
                 double length,
                 F&& getHeight)

with body in both case

{
  // magic
  double sampleIntervall = //from magic

    double dist = 0;
    while (length - dist > -0.1) {
        ret->push_back(dist, getHeight(dist));
        dist += sampleIntervall;
    }
}

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180990

What you can use is a std::function. It will wrap a callable type be that a function, member function, object with an overloaded function operator. You just need to supply the signature to use. So in you case you would have

void E::createEP(std::list<double>* ret, double length, std::function<double(double)> getHeight) 

Then to bind the member function with the object you want to call it on you can use a lambda like

[&](double val){ return object.FunctionName(val); }

or std::bind like

std::bind(&ClassName::FunctionName, &object)

Upvotes: 6

Hayt
Hayt

Reputation: 5370

std::function is really useful here

see this:

#include <functional>

using funType = std::function<void(double)>;
struct foo
{
  void bar(double d) {};
  static void call(funType f) {};
};

void foo2(double)
{
}

//...       
foo f;
//create lambda which calls member function
auto f1 = [&f](double d) { f.bar(4); };

foo::call(f1); //indirect call to member function due to lambda
foo::call(&foo2); //call to non-member function.
//...

std::function can hold a function pointer or a lambda which makes this really nice compared to c-style function pointers.

You can also call it with member functions directly but that requires bind. I think the solution with wrapping the call in a lambda looks a bit better for this.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

With c++11 the probably best option is to use std::function<double(double)> instead of the raw function pointer.

Upvotes: 0

Related Questions