Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a C++ Member Function Pointer to an STL Algorithm

I have a member function as follows:

class XYZ{
public:
    float function(float x);
private:
    float m_DensityMin;
    float m_DensityMax;
};

Now, I'm trying to transform a std::vector<float> foo using the std::transform STL algorithm by passing the member function function, and storing the resulting values in a vector bar.

If I use the function as a global function, with random data that should denote the member variables of the class, it works fine.

However, as the function requires the use of member variables m_DensityMin and m_DensityMax of the class, I need to use it as a member function. This is what I've tried:

std::transform(foo.begin(), foo.end(), bar.begin(), &XYZ::function);

but I end up with the error in VS2010:

error C2065: term does not evaluate to a function taking 1 arguments

As far as I can tell I'm passing only 1 argument. Any pointers? Here's a similar question, I've tried with std::mem_fun, as std::mem_fn is not available to me, but to no avail.

like image 570
ccoder83 Avatar asked May 20 '15 15:05

ccoder83


2 Answers

std::mem_fun itself only povides a wrapper for a member function, so that one will be invoked in the context of the first argument passed to its operator(). Having said that, you need to bind the proper instance of XYZ prior to passing the function object to the std::transform algorithm:

XYZ xyz;

std::transform(foo.begin(), foo.end(),
               std::back_inserter(bar),
               std::bind1st(std::mem_fun(&XYZ::function), &xyz));
//                  ~~~~~~^      ~~~~~~^                  ~~~^

DEMO

like image 77
Piotr Skotnicki Avatar answered Oct 05 '22 21:10

Piotr Skotnicki


Unless the function is static, it needs an instance of the class to call the function off of. Because non-static functions should depend on the internal state of that class. If the function is independent of the class's state, it should probably be static.

If you had to leave your class as written, you could get around this by default constructing an XYZ just for the purposes of having an instance to call the function from.

std::transform(foo.begin(),
               foo.end(),
               std::back_inserter(bar),
               [](float a){ return XYZ{}.function(a); });

But this feels hacky, I would prefer that the function be static if it isn't dependent on the state of the XYZ instance, and only depends on the input float.

like image 38
Cory Kramer Avatar answered Oct 05 '22 19:10

Cory Kramer